Skip to content

Commit

Permalink
Merge branch 'main' into dagger-cicd
Browse files Browse the repository at this point in the history
Signed-off-by: Bishal Das  <[email protected]>
  • Loading branch information
bishal7679 authored Mar 8, 2024
2 parents fbe4b2e + aab05ad commit e6a1678
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 12 deletions.
15 changes: 4 additions & 11 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,11 @@ go 1.21
require github.com/spf13/cobra v1.8.0

require (
github.com/99designs/gqlgen v0.17.31 // indirect
github.com/Khan/genqlient v0.6.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/rogpeppe/go-internal v1.11.0 // indirect
github.com/stretchr/testify v1.8.3 // indirect
github.com/vektah/gqlparser/v2 v2.5.6 // indirect
golang.org/x/exp v0.0.0-20231110203233-9a3e6036ecaa // indirect
golang.org/x/sync v0.6.0 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/testify v1.9.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

require (
Expand Down
3 changes: 2 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,8 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.8.3/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/tidwall/pretty v1.0.0 h1:HsD+QiTn7sK6flMKIvNmpqz1qrpP3Ps6jOKIKMooyg4=
github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
github.com/vektah/gqlparser v1.1.2 h1:ZsyLGn7/7jDNI+y4SEhI4yAxRChlv15pUHMjijT+e68=
Expand Down
149 changes: 149 additions & 0 deletions pkg/utils/credential_store_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
package utils

import (
"os"
"testing"

"github.com/stretchr/testify/require"
)

func TestCheckAndUpdateCredentialName(t *testing.T) {
// Test case 1: When credential name is already specified
credential := Credential{
Name: "testname",
Username: "testuser",
Password: "testpassword",
ServerAddress: "http://test-server",
}
checkAndUpdateCredentialName(&credential)
require.Equal(t, "testname", credential.Name, "Credential name should remain unchanged")

// Test case 2: When server address is a valid URL
validURLCredential := Credential{
Username: "testuser",
Password: "testpassword",
ServerAddress: "http://test-server",
}
checkAndUpdateCredentialName(&validURLCredential)
require.Equal(t, "test-server-testuser", validURLCredential.Name, "Credential name not updated correctly")
}

func TestReadCredentialStore(t *testing.T) {
// Test case 1: When config file does not exist
_, err := readCredentialStore()
require.Error(t, err, "Should return error when config file does not exist")

configFile = t.TempDir() + "/config"

// Test case 2: When config file exists
sampleCredentialYaml := "current-credential-name: test-credential\ncredentials:\n- name: test-credential\n username: testuser\n password: testpassword\n serveraddress: http://test-server"
os.WriteFile(configFile, []byte(sampleCredentialYaml), 0644)

credentialStore, err := readCredentialStore()
require.NoError(t, err, "Should not return error when config file exists")
require.Equal(t, 1, len(credentialStore.Credentials))
require.Equal(t, credentialStore.CurrentCredentialName, "test-credential")
require.Equal(t, credentialStore.Credentials[0], Credential{
Name: "test-credential",
Username: "testuser",
Password: "testpassword",
ServerAddress: "http://test-server",
})
}

func TestCheckAndCreateConfigFile(t *testing.T) {
// Test case 1: When config file does not exist
configFile = t.TempDir() + "/config"
checkAndCreateConfigFile()
_, err := os.Stat(configFile)
require.NoError(t, err, "Config file should be created")

// Test case 2: When config file already exists
checkAndCreateConfigFile()
_, err = os.Stat(configFile)
require.NoError(t, err, "Config file should not be created again")
}

func TestStoreCredential(t *testing.T) {
configFile = t.TempDir() + "/config"

// Test case 1: Should store the credential and set it as the current credential
credential := Credential{
Name: "test-credential-1",
Username: "testuser1",
Password: "testpassword1",
ServerAddress: "http://test-server1",
}
StoreCredential(credential, true)
credentialStore, err := readCredentialStore()
require.NoError(t, err)
require.Equal(t, "test-credential-1", credentialStore.CurrentCredentialName)
require.Equal(t, 1, len(credentialStore.Credentials))
require.Equal(t, credential, credentialStore.Credentials[0])

// Test case 2: Should store the credential but not set it as the current credential
credential = Credential{
Name: "test-credential-2",
Username: "testuser2",
Password: "testpassword2",
ServerAddress: "http://test-server2",
}
StoreCredential(credential, false)
credentialStore, err = readCredentialStore()
require.NoError(t, err)
require.Equal(t, "test-credential-1", credentialStore.CurrentCredentialName)
require.Equal(t, 2, len(credentialStore.Credentials))
require.Equal(t, credential, credentialStore.Credentials[1])

// Test case 3: Should update the name and password of stored credentials
credential = Credential{
Name: "new-name-1",
Username: "testuser1",
Password: "new-password-1",
ServerAddress: "http://test-server1",
}
StoreCredential(credential, true)
credentialStore, err = readCredentialStore()
require.NoError(t, err)
require.Equal(t, "new-name-1", credentialStore.CurrentCredentialName)
require.Equal(t, 2, len(credentialStore.Credentials))
require.Equal(t, credential, credentialStore.Credentials[1])
}

func TestResolveCredential(t *testing.T) {
// Define the credential that we expect to be resolved
requiredCrendential := Credential{
Name: "test-credential-1",
Username: "testuser1",
Password: "testpassword1",
ServerAddress: "http://test-server1",
}

// Populate the config file with some sample credentials of two different users
configFile = t.TempDir() + "/config"
sampleCredentialYaml := "current-credential-name: test-credential-1\ncredentials:\n- name: test-credential-1\n username: testuser1\n password: testpassword1\n serveraddress: http://test-server1 \n- name: test-credential-2\n username: testuser2\n password: testpassword2\n serveraddress: http://test-server2"
os.WriteFile(configFile, []byte(sampleCredentialYaml), 0644)

// Test case 1: When credential name is specified
resolvedCredential, err := resolveCredential("test-credential-1")
require.NoError(t, err)
require.Equal(t, requiredCrendential, resolvedCredential)

// Test case 2: When credential name is not specified, we should fetch it from the environment variable
os.Setenv("HARBOR_CREDENTIAL", "test-credential-1")
resolvedCredential, err = resolveCredential("")
require.NoError(t, err)
require.Equal(t, requiredCrendential, resolvedCredential)

// Test case 3: When credential name is not specified and environment variable is not set, we should use the current active credential
os.Unsetenv("HARBOR_CREDENTIAL")
resolvedCredential, err = resolveCredential("")
require.NoError(t, err)
require.Equal(t, requiredCrendential, resolvedCredential)

// Test case 4: When all of the above are not specified, we should return an error
os.WriteFile(configFile, []byte(""), 0644)
resolvedCredential, err = resolveCredential("")
require.Error(t, err)
require.Equal(t, Credential{}, resolvedCredential)
}

0 comments on commit e6a1678

Please sign in to comment.