-
Notifications
You must be signed in to change notification settings - Fork 706
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
[kubeops] Add tests for GetRelease #1434
Changes from 4 commits
5d1511a
244112c
a74e1b1
2fabd14
eb54b98
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
package handler | ||
|
||
import ( | ||
"net/http/httptest" | ||
"runtime/debug" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
"github.com/kubeapps/common/response" | ||
h3chart "helm.sh/helm/v3/pkg/chart" | ||
h3 "helm.sh/helm/v3/pkg/release" | ||
h2chart "k8s.io/helm/pkg/proto/hapi/chart" | ||
h2 "k8s.io/helm/pkg/proto/hapi/release" | ||
|
||
"testing" | ||
) | ||
|
||
func TestNewDashboardCompatibleRelease(t *testing.T) { | ||
type testScenario struct { | ||
// Scenario params | ||
Description string | ||
Helm3Release h3.Release | ||
Helm2Release h2.Release | ||
MarshallingFunction func(h2.Release) string | ||
} | ||
tests := []testScenario{ | ||
{ | ||
Description: "Two equivalent releases", | ||
MarshallingFunction: asResponse, | ||
Helm3Release: h3.Release{ | ||
Name: "Foo", | ||
Namespace: "default", | ||
Chart: &h3chart.Chart{ | ||
Metadata: &h3chart.Metadata{}, | ||
Values: map[string]interface{}{ | ||
"port": 8080, | ||
}, | ||
}, | ||
Info: &h3.Info{ | ||
Status: h3.StatusDeployed, | ||
}, | ||
Version: 1, | ||
Config: map[string]interface{}{ | ||
"port": 3000, | ||
"user": map[string]interface{}{ | ||
"name": "user1", | ||
"password": "123456", | ||
}, | ||
}, | ||
}, | ||
Helm2Release: h2.Release{ | ||
Name: "Foo", | ||
Namespace: "default", | ||
Info: &h2.Info{ | ||
Status: &h2.Status{ | ||
Code: h2.Status_DEPLOYED, | ||
}, | ||
}, | ||
Chart: &h2chart.Chart{ | ||
Metadata: &h2chart.Metadata{}, | ||
Values: &h2chart.Config{ | ||
Raw: "port: 8080\n", | ||
}, | ||
}, | ||
Version: 1, | ||
Config: &h2chart.Config{ | ||
Raw: "port: 3000\nuser:\n name: user1\n password: \"123456\"\n", | ||
}, | ||
}, | ||
}, | ||
{ | ||
Description: "Two equivalent releases with switched order of values", | ||
MarshallingFunction: asResponse, | ||
Helm3Release: h3.Release{ | ||
Name: "Foo", | ||
Namespace: "default", | ||
Chart: &h3chart.Chart{ | ||
Metadata: &h3chart.Metadata{}, | ||
Values: map[string]interface{}{}, | ||
}, | ||
Info: &h3.Info{ | ||
Status: h3.StatusDeployed, | ||
}, | ||
Version: 1, | ||
Config: map[string]interface{}{ | ||
"user": map[string]interface{}{ | ||
"password": "123456", | ||
"name": "user1", | ||
}, | ||
"port": 3000, | ||
}, | ||
}, | ||
Helm2Release: h2.Release{ | ||
Name: "Foo", | ||
Namespace: "default", | ||
Info: &h2.Info{ | ||
Status: &h2.Status{ | ||
Code: h2.Status_DEPLOYED, | ||
}, | ||
}, | ||
Chart: &h2chart.Chart{ | ||
Metadata: &h2chart.Metadata{}, | ||
Values: &h2chart.Config{ | ||
Raw: "{}\n", | ||
}, | ||
}, | ||
Version: 1, | ||
Config: &h2chart.Config{ | ||
Raw: "port: 3000\nuser:\n name: user1\n password: \"123456\"\n", | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.Description, func(t *testing.T) { | ||
// Capture the panic and report it in an orderly fashion | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm confused as to why we're handling a panic that we've discovered in tests, rather than removing the panic from the code? What causes the panic... if I run these tests without the panic handling, it just succeeds. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I came back to this later today, found the cause of the panic, added some failing tests and fixed. See what you think: #1445 |
||
defer func() { | ||
if r := recover(); r != nil { | ||
t.Errorf("Got a panic: %v. \nStacktrace: \n%s", r, string(debug.Stack())) | ||
} | ||
}() | ||
// Perform conversion | ||
compatibleH3rls := newDashboardCompatibleRelease(test.Helm3Release) | ||
// Marshall both: Compatible H3Release and H2Release | ||
h3Marshalled := test.MarshallingFunction(compatibleH3rls) | ||
t.Logf("Marshalled Helm 3 Release %s", h3Marshalled) | ||
h2Marshalled := test.MarshallingFunction(test.Helm2Release) | ||
t.Logf("Marshalled Helm 2 Release %s", h2Marshalled) | ||
// Check result | ||
if h3Marshalled != h2Marshalled { | ||
t.Errorf("Not equal: %s", cmp.Diff(h3Marshalled, h2Marshalled)) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func asResponse(data h2.Release) string { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you explain why you are using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. still not sure about this ^ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the current setting, the result from the function If my reasoning seems too stretched I can just bake it into the test. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's fine, a comment explaining that would be helpful for the future |
||
w := httptest.NewRecorder() | ||
response.NewDataResponse(data).Write(w) | ||
return w.Body.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.
this is always
asResponse
, in which case you plan to change this function?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.
If we add a test scenario where we test
newDashboardCompatibleRelease
against for examplespew.Sdump
, or a hand-written one where for example not all fields are relevant/populated etc.