Skip to content

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

Merged
merged 38 commits into from
Jan 12, 2022
Merged

feat: add unit tests #5

merged 38 commits into from
Jan 12, 2022

Conversation

xoanmm
Copy link
Contributor

@xoanmm xoanmm commented Dec 27, 2021

No description provided.

@xoanmm xoanmm requested a review from a team December 27, 2021 16:23
@xoanmm xoanmm self-assigned this Dec 27, 2021
@xoanmm xoanmm requested review from lupiuz, mknet3 and ishioni and removed request for a team December 27, 2021 16:23
@xoanmm xoanmm linked an issue Dec 27, 2021 that may be closed by this pull request
Copy link
Contributor

@JorTurFer JorTurFer left a 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

Comment on lines 31 to 43
- 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
Copy link
Contributor

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

Copy link
Contributor Author

@xoanmm xoanmm Jan 12, 2022

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.

Comment on lines 1 to 16
#!/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
Copy link
Contributor

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

Copy link
Contributor Author

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]>
Copy link
Contributor

@patrykwozinski patrykwozinski left a 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.

Comment on lines 20 to 26
if g.SSHPrivKey != "" {

return git.NewSSHCreds(g.SSHPrivKey, "", true), nil
} else {
return nil, fmt.Errorf(
"sshPrivKey not provided for authenticatication to repository %s",
repoURL,
)
Copy link
Contributor

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.

Copy link
Contributor

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

Copy link
Contributor Author

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

Comment on lines 31 to 35
} else {
return nil, fmt.Errorf(
"no value provided for username and password for authentication to repository %s",
repoURL,
)
Copy link
Contributor

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.

Copy link
Contributor Author

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) {
Copy link
Contributor

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!

Copy link
Contributor Author

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

Comment on lines 69 to 71
if !(reflect.DeepEqual(creds, expectedCreds)) {
log.Fatalf("creds and expectedCreds are equal: %t\n", reflect.DeepEqual(creds, expectedCreds))
}
Copy link
Contributor

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.

Copy link
Contributor Author

@xoanmm xoanmm Jan 12, 2022

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{}))
}

Copy link
Contributor

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!

Password: validGitCredentialsPassword,
}

repoURL := validGitRepoHTTPSURL
Copy link
Contributor

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?

Copy link
Contributor Author

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

@@ -17,15 +18,23 @@ type Credentials struct {
func (g Credentials) NewCreds(repoURL string) (git.Creds, error) {
Copy link
Contributor

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.

Copy link
Contributor Author

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?

const validHelmAppName = "example-app"
const validHelmAppFileToChange = validHelmAppName + "/values.yaml"

func getRouteRelativePath(numRelativePath int, relativePath string) (*string, error) {
Copy link
Contributor

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. :)

Copy link
Contributor Author

@xoanmm xoanmm Jan 12, 2022

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?

Comment on lines 42 to 45
func TestMain(m *testing.M) {
code := m.Run()
os.Exit(code)
}
Copy link
Contributor

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.

Copy link
Contributor Author

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
Copy link
Contributor

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?

Comment on lines 60 to 64
tmpFileName := tmpFile.Name()
err = ioutil.WriteFile(tmpFileName, yamlData, 0644)
if err != nil {
return nil, err
}
Copy link
Contributor

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?

@xoanmm xoanmm merged commit 6a62eb5 into develop Jan 12, 2022
@xoanmm xoanmm deleted the feature/create-unit-tests branch January 12, 2022 15:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

add unit tests and update Github Actions adding it
5 participants