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

Tests for CreateService and annotations #246

Merged
merged 1 commit into from
Nov 1, 2016

Conversation

cdrage
Copy link
Member

@cdrage cdrage commented Oct 27, 2016

Adds some tests for CreateService, specifically the initial generation
as well as providing an edge case when specifying "kompose.service.type"

@cdrage
Copy link
Member Author

cdrage commented Oct 27, 2016

Fixes 1/2 of #242

@cdrage
Copy link
Member Author

cdrage commented Oct 27, 2016

Still newbish to Go, so let me know what things I can improve on to make this more idiomatic!

@cdrage cdrage force-pushed the add-tests-docs-service-types branch from 2de342a to b69b16a Compare October 27, 2016 14:36
Copy link
Member

@kadel kadel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm don't know that much about writing tests :-(

@kadel kadel added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Oct 30, 2016
objects := k.Transform(kompose_object, kobject.ConvertOptions{CreateD: true, Replicas: 3})

// Test the creation of the service
CreateService("foo", service, objects)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't be there at least some kind of test that service has been created?
Maybe I just don't understand it, but it looks like this is not testing anything :-)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kadel you're right, i forgot to add the checking err in this test, was initially a copy-and-paste from the test below. thank you!

@kadel kadel removed the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Oct 30, 2016
@cdrage cdrage force-pushed the add-tests-docs-service-types branch from b69b16a to 6484d83 Compare October 31, 2016 15:17
@cdrage
Copy link
Member Author

cdrage commented Oct 31, 2016

@kadel me neither for the tests, I updated it to check that information is generated. I tried doing a comparison between port numbers (passing 123 and making sure we get 123 back). But I'm having a bit of difficulty digging through k8s code to find the correct way of doing it. Hopefully this update to the PR is sufficient!

@kadel
Copy link
Member

kadel commented Oct 31, 2016

there already is a function for comparing ports in kubernetes_test.go

@kadel
Copy link
Member

kadel commented Oct 31, 2016

you commited vim swap file ;-) - pkg/transformer/kubernetes/.k8sutils_test.go.swp

@cdrage cdrage force-pushed the add-tests-docs-service-types branch 2 times, most recently from 862ac98 to 320d50c Compare October 31, 2016 18:17
@cdrage
Copy link
Member Author

cdrage commented Oct 31, 2016

@kadel boo, that's why I added the .gitignore PR earlier xD okay, so i've updated this PR again with new code, although I'm not sure why the tests are erroring.

Updated the PR, passes now! Ready for another review.

@cdrage cdrage force-pushed the add-tests-docs-service-types branch 2 times, most recently from 88a7e79 to f972059 Compare October 31, 2016 18:56

// Test the creation of the service with modified annotations (kompose.service.type)
svc := k.CreateService("foo", service, objects)
if svc.Spec.Type != api.ServiceType("NodePort") {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One last thing ;-)
You test only NodePort, it would be great to test also other options for service.type.
This might be great case for something like this: https://github.com/golang/go/wiki/TableDrivenTests

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kadel sure, i'll work on that. i'll include it in the top test however since this test is specific to supplying nodeport (testing the annotations / labels 👍 )

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kadel so I don't think I can compare anything else (svc.Spec.Type is blank, same as svc.Spec.ClusterIP, svc.Spec.LoadBalancerIP, etc.)

Should I just compare that it comes up as blank?

Copy link
Member

@kadel kadel Nov 1, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cdrage sorry my comment might be little bit confusing. I should do it as line comment.

What I meant was that it might be nice to make this test more generic. Using table driven tests you can do test that will test all possible values for kompose.service.type in one test. Basicaly create more services with different kompose.service.type label and test them all

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can do something like this to test all possible label values:

    // An example object generated via k8s runtime.Objects()
    kompose_object := kobject.KomposeObject{
        ServiceConfigs: map[string]kobject.ServiceConfig{"app": service},
    }
    k := Kubernetes{}

    tests := []struct {
        labelValue  string
        serviceType string
    }{
        {"NodePort", "NodePort"},
        {"nodeport", "NodePort"},
        {"LoadBalancer", "LoadBalancer"},
        {"loadbalancer", "LoadBalancer"},
        {"ClusterIP", "ClusterIP"},
        {"clusterip", "ClusterIP"},
    }

    for _, tt := range tests {
        kompose_object.ServiceConfigs["app"].Annotations["kompose.service.type"] = tt.labelValue

        objects := k.Transform(kompose_object, kobject.ConvertOptions{CreateD: true, Replicas: 3})

        // Test the creation of the service with modified annotations (kompose.service.type)
        svc := k.CreateService("foo", service, objects)
        if svc.Spec.Type != api.ServiceType(tt.serviceType) {
            t.Errorf("Expected NodePort, actual %d", svc.Spec.Type)
        }
    }

It would be great if we could also test invalid values, but it is not currently possible as CreateService logs error using logrus.Errorf instead of returning it :-(

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kadel AHHHH that makes sense! Thought you wanted to test for different keys, rather than different values. I'll add that! Thanks for doing the work for it too haha

@cdrage cdrage force-pushed the add-tests-docs-service-types branch from f972059 to 3c4b3c4 Compare November 1, 2016 14:37
@cdrage
Copy link
Member Author

cdrage commented Nov 1, 2016

@kadel updated with your code :) thanks man!

Adds some tests for CreateService, specifically the initial generation
as well as providing an edge case when specifying "kompose.service.type"
@kadel
Copy link
Member

kadel commented Nov 1, 2016

@kadel updated with your code :) thanks man!

thanks ;-)
Sorry for confusion, sometimes it is easier for me to express myself in code than in english 😆

@kadel kadel merged commit a3495b1 into kubernetes:master Nov 1, 2016
@cdrage
Copy link
Member Author

cdrage commented Nov 1, 2016

@kadel

01101110 01101111 00100000 01110000 01110010 01101111 01100010 01101100 01100101 01101101

:)

@kadel
Copy link
Member

kadel commented Nov 1, 2016

00111010 01000100

@cdrage cdrage deleted the add-tests-docs-service-types branch March 30, 2017 13:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants