-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add unit tests #5
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
Conversation
…git-server for tests
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In general, LGTM.
Only small nits
- name: View all in port 22 | ||
run: | | ||
sudo lsof -i -P -n | grep 22 | ||
sudo lsof -i:22 | ||
|
||
- name: Kill all in port 22 | ||
run: | | ||
sudo fuser -k 22/tcp | ||
|
||
- name: View all in port 22 | ||
run: | | ||
sudo lsof -i -P -n | grep 22 | ||
sudo lsof -i:22 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are these lines needed? In agent runs from scratch, and you shouldn't kill anything there :/
Maybe we should change the port that we use if it's in conflict with ssh server port
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was only a quick test, and really was a bad idea. Now all the actions related with tests have been modified in Makefile
to start a container that will create a simple git server with a repo created from scratch and it will be exposed in port 2222.
launch_test_env.sh
Outdated
#!/bin/bash | ||
|
||
ACTION="${1}" | ||
|
||
if [[ "$ACTION" == "create" ]]; then | ||
echo "It's going to launch actions for create tests environment" | ||
# Create git-server | ||
docker-compose -f test-git-server/docker-compose.yaml up -d --build git-server | ||
# Create repo in git-server | ||
docker-compose -f test-git-server/docker-compose.yaml up --build create-repo | ||
elif [[ "$ACTION" == "destroy" ]]; then | ||
# Delete git-server and create-repo containers | ||
docker-compose -f test-git-server/docker-compose.yaml up down | ||
else | ||
echo "ACTION provided $ACTION is not a valid command. The only options available are 'create' and 'destroy'" | ||
fi |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be handled by make instead another script? I mean, something like make launch
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right, updated the Makefile
for launch as make launch-test-deps
, this will be launched also as first step of any target of make related with tests
Co-authored-by: Jorge Turrado Ferrero <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @xoanmm! I reviewed .go
files and left some minor questions/suggestions/comments. Feel free to ignore them if something is not valid, I'm not an expert of Go nor this context of the HelmRepoUpdater. No changes requested, only comments-like things.
internal/app/git/creds.go
Outdated
if g.SSHPrivKey != "" { | ||
|
||
return git.NewSSHCreds(g.SSHPrivKey, "", true), nil | ||
} else { | ||
return nil, fmt.Errorf( | ||
"sshPrivKey not provided for authenticatication to repository %s", | ||
repoURL, | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In such cases, in my opinion, it's always better to avoid using the else
statement. For example you can do:
if g.SSHPrivKey == "" {
return nil, fmt.Errorf(
"sshPrivKey not provided for authenticatication to repository %s",
repoURL,
)
}
return git.NewSSHCreds(g.SSHPrivKey, "", true), nil
Sometimes it's called "early exit".
What do you think @xoanmm? It's not a change request, only a suggestion to consider.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AFAIK, else it's bad practice in golang and usually linters throw an error
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, you're right, correcting just now to update in next commit. Thanks :D
internal/app/git/creds.go
Outdated
} else { | ||
return nil, fmt.Errorf( | ||
"no value provided for username and password for authentication to repository %s", | ||
repoURL, | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same in the previous comment. Maybe it's worth considering if early exit
is not fitting here instead of the else
statement.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, you're right, correcting just now to update in next commit. Thanks :D
|
||
} | ||
|
||
// commitChangesLocked commits the changes to the git repository. | ||
func commitChangesLocked(cfg HelmUpdaterConfig, state *SyncIterationState) error { | ||
func commitChangesLocked(cfg HelmUpdaterConfig, state *SyncIterationState) (*[]ChangeEntry, error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we return a value []ChangeEntry
instead of the pointer here? It is possible, or it's nonsense? Sorry for the dumb question!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this case because normally when are returned pairs like Struct, error
and there is the possibility that error is not nil is returned the first object as nil and for that is need to declared as pointer, in this case really because the return of the function is a call to function commitChangesGit
that need to be able to return nil as the first object
internal/app/git/creds_test.go
Outdated
if !(reflect.DeepEqual(creds, expectedCreds)) { | ||
log.Fatalf("creds and expectedCreds are equal: %t\n", reflect.DeepEqual(creds, expectedCreds)) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, I'm wondering if it's not worth using stretchr/testify
for such cases. I mean - making an inversed if
statement is solid, but it's not that clear from my point of view for code-readers.
You can read more here.
Of course, it's not necessary, but that might somehow improve the readability of tests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right, is better use an assert approach, I didn't know about stretchr/testify
, for that type of tests normally I use library "gotest.tools/v3/assert"
, and this test now looks like the following:
func TestNewCredsSSHURLSShPrivKey(t *testing.T) {
g := Credentials{
Email: validGitCredentialsEmail,
SSHPrivKey: validGitCredentialsSSHKey,
}
repoURL := validGitRepoSSHURL
creds, err := g.NewCreds(repoURL)
if err != nil {
log.Fatal(err)
}
expectedCreds := git.NewSSHCreds(validGitCredentialsSSHKey, "", true)
assert.DeepEqual(t, creds, expectedCreds, cmp.AllowUnexported(git.SSHCreds{}))
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice, it's cleaner I think now! Thank you!
internal/app/git/creds_test.go
Outdated
Password: validGitCredentialsPassword, | ||
} | ||
|
||
repoURL := validGitRepoHTTPSURL |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this temp variable necessary? Maybe we can use validGitRepoHTTPSURL
directly for NewCreeds
? What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, you're right, is not necessary declare a local variable for that, it can be used validGitRepoHTTPSURL
for call NewCreds
. Updating for upload in next commit
internal/app/git/creds.go
Outdated
@@ -17,15 +18,23 @@ type Credentials struct { | |||
func (g Credentials) NewCreds(repoURL string) (git.Creds, error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not related to this PR (!).
In Golang NewSomething
is an approach to create a new instance of something (static factory method). For example, if you have a GitHubClient
struct, and you want to create a new instance of it, then you should have a function called NewGitHubClient
. In our case - this function is something for fetching credentials from the git
library (whatever it is) so maybe it's better to rename it in the future to something more clear.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe it will be a good idea change to NewGitCreds
, wdyt?
internal/app/updater/commit_test.go
Outdated
const validHelmAppName = "example-app" | ||
const validHelmAppFileToChange = validHelmAppName + "/values.yaml" | ||
|
||
func getRouteRelativePath(numRelativePath int, relativePath string) (*string, error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's really, really not an important comment/suggestion, but private functions IMO should be on the bottom of files. Thanks to that we'll hide the complexity, and the reviewer will not focus on it at the beginning, and that's fine because it's an implementation detail. :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree, and in this case I'll move this function to a new package called utils, and also convert into public function, because it will be necessary add more and used it in different internal packages. wdyt?
internal/app/updater/commit_test.go
Outdated
func TestMain(m *testing.M) { | ||
code := m.Run() | ||
os.Exit(code) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the purpose of this function? I mean, os.Exit(m.Run())
is not a default implementation of the TestMain
function?
From my point of view (I'm so f***cking noob in Go) this should be reserved for bootstrapping more complex tests where you need to set up something before tests and it's repeatable for all test functions in it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Initially I thought it might be necessary to set up extra things but it has not been necessary, so I will remove this feature from all tests for the time being.
@@ -52,40 +52,39 @@ func overrideValues(cfg HelmUpdaterConfig, targetFile string) ([]ChangeEntry, er | |||
for _, app := range cfg.UpdateApps { | |||
// define new entry | |||
var newEntry ChangeEntry | |||
var oldValue, newValue string | |||
var oldValue, newValue *string |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we need a pointer here?
internal/app/yq/commands_test.go
Outdated
tmpFileName := tmpFile.Name() | ||
err = ioutil.WriteFile(tmpFileName, yamlData, 0644) | ||
if err != nil { | ||
return nil, err | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't need to close this file in such a case? Maybe it's worth doing a defer
for it?
No description provided.