Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated the test files #236

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions cmd/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package main

import "testing"

func Test_main(t *testing.T) {
tests := []struct {
name string
}{}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
main()
})
}
}
188 changes: 118 additions & 70 deletions pkg/agent/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,33 @@ import (
"capten/pkg/agent/pb/agentpb"
"capten/pkg/agent/pb/vaultcredpb"
"capten/pkg/config"
"crypto/tls"
"fmt"
"log"
"os"

//"reflect"
"path/filepath"
"strings"
"testing"

"github.com/stretchr/testify/assert"
"google.golang.org/grpc/credentials"
)

func TestGetAgentClient(t *testing.T) {

currentdir, err := os.Getwd()
if err != nil {
log.Println("Error while getting cuerent dir", err)
}
presentdir, err := getRelativePathUpTo(currentdir)

if err != nil {
log.Println("Error while getting relative path", err)
}

type args struct {
config config.CaptenConfig
config config.CaptenConfig
clusterconf config.CaptenClusterValues
}
tests := []struct {
name string
Expand All @@ -23,13 +39,22 @@ func TestGetAgentClient(t *testing.T) {
wantErr bool
}{
{

name: "Secure connection",
args: args{
config: config.CaptenConfig{
AgentSecure: true,
AgentHostName: "captenagent",
// DomainName: "com",
// CaptenAgentPort: "50051",
AgentSecure: true,
AgentHostName: "captenagent",
CertDirPath: "/" + presentdir + "/cert/",
ClientKeyFileName: "client.key",
ClientCertFileName: "client.crt",
CAFileName: "ca.crt",
CaptenClusterHost: config.CaptenClusterHost{
LoadBalancerHost: "a084c23852d0b428e98f363457fc8f8b-5ee99283c8b044fa.elb.us-west-2.amazonaws.com",
},
},
clusterconf: config.CaptenClusterValues{
DomainName: "awsdemo.optimizor.app",
},
},
wantErr: false,
Expand All @@ -40,13 +65,13 @@ func TestGetAgentClient(t *testing.T) {
config: config.CaptenConfig{
AgentSecure: false,
AgentHostName: "captenagent",
// DomainName: "com",
// CaptenAgentPort: "50051",
},
clusterconf: config.CaptenClusterValues{
DomainName: "awsdemo.optimizor.app",
},
},
wantErr: false,
},
// Add more test cases as needed
}

for _, tt := range tests {
Expand All @@ -68,6 +93,17 @@ func TestGetAgentClient(t *testing.T) {
}

func TestGetVaultClient(t *testing.T) {

currentdir, err := os.Getwd()
if err != nil {
log.Println("Error while getting cuerent dir", err)
}
dir, err := getRelativePathUpTo(currentdir)

if err != nil {
log.Println("Error while getting working dir", err)
}

type args struct {
config config.CaptenConfig
}
Expand All @@ -81,8 +117,16 @@ func TestGetVaultClient(t *testing.T) {
name: "Secure connection",
args: args{
config: config.CaptenConfig{
AgentSecure: true,
AgentHostName: "captenagent",
AgentSecure: true,
AgentHostName: "captenagent",
CertDirPath: "/" + dir + "/cert/",
ClientKeyFileName: "client.key",
ClientCertFileName: "client.crt",
CAFileName: "ca.crt",
VaultCredHostName: "vault-cred",
CaptenClusterHost: config.CaptenClusterHost{
LoadBalancerHost: "a084c23852d0b428e98f363457fc8f8b-5ee99283c8b044fa.elb.us-west-2.amazonaws.com",
},
},
},
wantErr: false,
Expand Down Expand Up @@ -127,80 +171,84 @@ func TestGetVaultClient(t *testing.T) {

}

func TestLoadTLSCredentials(t *testing.T) {
// Test case 1: LoadX509KeyPair fails
captenConfig := config.CaptenConfig{
//CertDirPath: "/path/to/certs",
ClientCertFileName: "client.crt",
ClientKeyFileName: "client.key",
CAFileName: "ca.crt",
}
os.MkdirAll(captenConfig.CertDirPath, os.ModePerm)
defer os.RemoveAll(captenConfig.CertDirPath)

_, err := loadTLSCredentials(captenConfig)
if err == nil {
t.Errorf("Expected error, got nil")
}

// Test case 2: AppendCertsFromPEM fails
certFile := captenConfig.PrepareFilePath(captenConfig.CertDirPath, captenConfig.ClientCertFileName)
keyFile := captenConfig.PrepareFilePath(captenConfig.CertDirPath, captenConfig.ClientKeyFileName)
caFile := captenConfig.PrepareFilePath(captenConfig.CertDirPath, captenConfig.CAFileName)

err = os.WriteFile(certFile, []byte("dummy cert"), os.ModePerm)
if err != nil {
t.Fatalf("Failed to write cert file: %v", err)
}
func getRelativePathUpTo(currentPath string) (string, error) {
targetDir := "capten"
parts := strings.Split(currentPath, string(filepath.Separator))

err = os.WriteFile(keyFile, []byte("dummy key"), os.ModePerm)
if err != nil {
t.Fatalf("Failed to write key file: %v", err)
for i, part := range parts {
if part == targetDir {
return filepath.Join(parts[:i+1]...), nil
}
}

err = os.WriteFile(caFile, []byte("dummy ca"), os.ModePerm)
if err != nil {
t.Fatalf("Failed to write ca file: %v", err)
}
return "", fmt.Errorf("directory %s not found in path %s", targetDir, currentPath)
}

_, err = loadTLSCredentials(captenConfig)
if err == nil {
t.Errorf("Expected error, got nil")
}
func Test_loadTLSCredentials(t *testing.T) {

// Test case 3: Successful load
certPEM, err := os.ReadFile("testdata/cert.pem")
currentdir, err := os.Getwd()
if err != nil {
t.Fatalf("Failed to read cert file: %v", err)
log.Println("Error while getting cuerent dir", err)
}
dir, err := getRelativePathUpTo(currentdir)

keyPEM, err := os.ReadFile("testdata/key.pem")
if err != nil {
t.Fatalf("Failed to read key file: %v", err)
log.Println("Error while getting working dir", err)
}

caPEM, err := os.ReadFile("testdata/ca.pem")
if err != nil {
t.Fatalf("Failed to read ca file: %v", err)
type args struct {
captenConfig config.CaptenConfig
}

err = os.WriteFile(certFile, certPEM, os.ModePerm)
if err != nil {
t.Fatalf("Failed to write cert file: %v", err)
}
tests := []struct {
name string
args args
want credentials.TransportCredentials
wantErr bool
}{
{
name: "valid captenConfig",
args: args{
captenConfig: config.CaptenConfig{
AgentCertFileName: "agent.crt",
AgentKeyFileName: "agent.key",
CAFileName: "ca.crt",
CertDirPath: "/" + dir + "/cert/",
ClientCertFileName: "client.crt",
ClientKeyFileName: "client.key",
},
},
want: credentials.NewTLS(&tls.Config{

err = os.WriteFile(keyFile, keyPEM, os.ModePerm)
if err != nil {
t.Fatalf("Failed to write key file: %v", err)
}
ClientAuth: tls.RequireAnyClientCert,
}),

err = os.WriteFile(caFile, caPEM, os.ModePerm)
if err != nil {
t.Fatalf("Failed to write ca file: %v", err)
wantErr: false,
},
{
name: "invalid cert file",
args: args{
captenConfig: config.CaptenConfig{
AgentCertFileName: "client.crt",
AgentKeyFileName: "client.key",
CAFileName: "ca.crt",
CertDirPath: "/" + dir + "/cert/",
ClientCertFileName: "dbcjd.key",
ClientKeyFileName: "client.key",
},
},
want: nil,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := loadTLSCredentials(tt.args.captenConfig)
if (err != nil) != tt.wantErr {
t.Errorf("loadTLSCredentials() error = %v, wantErr %v", err, tt.wantErr)
return
}

_, err = loadTLSCredentials(captenConfig)
if err != nil {
t.Errorf("Expected no error, got %v", err)
})
}
}
Loading