-
Notifications
You must be signed in to change notification settings - Fork 54
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
Refactoring Pact tests and adding parametrized states #391
Changes from 1 commit
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,201 @@ | ||
// | ||
// Copyright 2023 Red Hat, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package contracts | ||
|
||
import ( | ||
"context" | ||
"strings" | ||
"time" | ||
|
||
gomega "github.com/onsi/gomega" | ||
models "github.com/pact-foundation/pact-go/v2/models" | ||
appstudiov1alpha1 "github.com/redhat-appstudio/application-api/api/v1alpha1" | ||
|
||
"k8s.io/apimachinery/pkg/types" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/envtest" | ||
) | ||
|
||
var ( | ||
k8sClient client.Client | ||
testEnv *envtest.Environment | ||
ctx context.Context | ||
cancel context.CancelFunc | ||
) | ||
|
||
type Comp struct { | ||
app AppParams | ||
repo string | ||
name string | ||
} | ||
|
||
type CompParams struct { | ||
components []Comp | ||
} | ||
|
||
type AppParams struct { | ||
appName string | ||
namespace string | ||
} | ||
|
||
const timeout = 10 * time.Second | ||
const interval = 250 * time.Millisecond | ||
|
||
// Deprecated | ||
func createAppAndComponents(HASAppNamespace string) models.StateHandler { | ||
var stateHandler = func(setup bool, s models.ProviderState) (models.ProviderStateResponse, error) { | ||
if !setup { | ||
println("skipping state handler") | ||
return nil, nil | ||
} | ||
|
||
appName := "myapp" | ||
ghCompName := "gh-component" | ||
quayCompName := "quay-component" | ||
ghCompRepoLink := "https://github.com/devfile-samples/devfile-sample-java-springboot-basic" | ||
quayRepoLink := "quay.io/test/test-image:latest" | ||
|
||
hasApp := getApplicationSpec(appName, HASAppNamespace) | ||
ghComp := getGhComponentSpec(ghCompName, HASAppNamespace, appName, ghCompRepoLink) | ||
quayComp := getQuayComponentSpec(quayCompName, HASAppNamespace, appName, quayRepoLink) | ||
|
||
//create app | ||
gomega.Expect(k8sClient.Create(ctx, hasApp)).Should(gomega.Succeed()) | ||
hasAppLookupKey := types.NamespacedName{Name: appName, Namespace: HASAppNamespace} | ||
createdHasApp := &appstudiov1alpha1.Application{} | ||
for i := 0; i < 12; i++ { | ||
gomega.Expect(k8sClient.Get(context.Background(), hasAppLookupKey, createdHasApp)).Should(gomega.Succeed()) | ||
if len(createdHasApp.Status.Conditions) > 0 { | ||
if createdHasApp.Status.Conditions[0].Type == "Created" { | ||
break | ||
} | ||
} | ||
time.Sleep(10 * time.Second) | ||
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. Please use eventually, bad practice to use sleep in tests |
||
} | ||
|
||
//create gh component | ||
gomega.Expect(k8sClient.Create(ctx, ghComp)).Should(gomega.Succeed()) | ||
hasCompLookupKey := types.NamespacedName{Name: ghCompName, Namespace: HASAppNamespace} | ||
createdHasComp := &appstudiov1alpha1.Component{} | ||
for i := 0; i < 12; i++ { | ||
gomega.Expect(k8sClient.Get(context.Background(), hasCompLookupKey, createdHasComp)).Should(gomega.Succeed()) | ||
if len(createdHasComp.Status.Conditions) > 1 { | ||
break | ||
} | ||
time.Sleep(10 * time.Second) | ||
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. Please use eventually, bad practice to use sleep in tests |
||
} | ||
//create quay component | ||
gomega.Expect(k8sClient.Create(ctx, quayComp)).Should(gomega.Succeed()) | ||
hasCompLookupKey2 := types.NamespacedName{Name: quayCompName, Namespace: HASAppNamespace} | ||
createdHasComp2 := &appstudiov1alpha1.Component{} | ||
for i := 0; i < 12; i++ { | ||
gomega.Expect(k8sClient.Get(context.Background(), hasCompLookupKey2, createdHasComp2)).Should(gomega.Succeed()) | ||
if len(createdHasComp2.Status.Conditions) > 1 { | ||
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. if conditions are not created this will result in panic 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. My bad. I saw something wron |
||
break | ||
} | ||
time.Sleep(10 * time.Second) | ||
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. Please use eventually, bad practice to use sleep in tests |
||
} | ||
|
||
for i := 0; i < 12; i++ { | ||
gomega.Expect(k8sClient.Get(context.Background(), hasAppLookupKey, createdHasApp)).Should(gomega.Succeed()) | ||
if len(createdHasApp.Status.Conditions) > 0 && strings.Contains(createdHasApp.Status.Devfile, ghCompName) { | ||
break | ||
} | ||
time.Sleep(10 * time.Second) | ||
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. Please use eventually, bad practice to use sleep in tests |
||
} | ||
return nil, nil | ||
} | ||
return stateHandler | ||
} | ||
|
||
func createApp() models.StateHandler { | ||
var stateHandler = func(setup bool, s models.ProviderState) (models.ProviderStateResponse, error) { | ||
if !setup { | ||
println("skipping state handler during turndownn phase") | ||
return nil, nil | ||
} | ||
|
||
app := parseApp(s.Parameters) | ||
hasApp := getApplicationSpec(app.appName, app.namespace) | ||
|
||
//create app | ||
gomega.Expect(k8sClient.Create(ctx, hasApp)).Should(gomega.Succeed()) | ||
hasAppLookupKey := types.NamespacedName{Name: app.appName, Namespace: app.namespace} | ||
createdHasApp := &appstudiov1alpha1.Application{} | ||
|
||
gomega.Eventually(func() bool { | ||
k8sClient.Get(context.Background(), hasAppLookupKey, createdHasApp) | ||
Check warning Code scanning / gosec Errors unhandled.
Errors unhandled.
|
||
return len(createdHasApp.Status.Conditions) > 0 | ||
}, timeout, interval).Should(gomega.BeTrue()) | ||
|
||
return nil, nil | ||
} | ||
return stateHandler | ||
} | ||
|
||
func createComponents() models.StateHandler { | ||
var stateHandler = func(setup bool, s models.ProviderState) (models.ProviderStateResponse, error) { | ||
if !setup { | ||
println("skipping state handler") | ||
return nil, nil | ||
} | ||
|
||
components := parseComp(s.Parameters) | ||
|
||
for _, comp := range components.components { | ||
ghComp := getGhComponentSpec(comp.name, comp.app.namespace, comp.app.appName, comp.repo) | ||
|
||
hasAppLookupKey := types.NamespacedName{Name: comp.app.appName, Namespace: comp.app.namespace} | ||
createdHasApp := &appstudiov1alpha1.Application{} | ||
|
||
//create gh component | ||
gomega.Expect(k8sClient.Create(ctx, ghComp)).Should(gomega.Succeed()) | ||
hasCompLookupKey := types.NamespacedName{Name: comp.name, Namespace: comp.app.namespace} | ||
createdHasComp := &appstudiov1alpha1.Component{} | ||
gomega.Eventually(func() bool { | ||
k8sClient.Get(context.Background(), hasCompLookupKey, createdHasComp) | ||
Check warning Code scanning / gosec Errors unhandled.
Errors unhandled.
|
||
return len(createdHasComp.Status.Conditions) > 1 | ||
}, timeout, interval).Should(gomega.BeTrue()) | ||
|
||
gomega.Eventually(func() bool { | ||
k8sClient.Get(context.Background(), hasAppLookupKey, createdHasApp) | ||
Check warning Code scanning / gosec Errors unhandled.
Errors unhandled.
|
||
return len(createdHasApp.Status.Conditions) > 0 && strings.Contains(createdHasApp.Status.Devfile, comp.name) | ||
}, timeout, interval).Should(gomega.BeTrue()) | ||
} | ||
return nil, nil | ||
} | ||
return stateHandler | ||
} | ||
|
||
func parseApp(params map[string]interface{}) AppParams { | ||
return AppParams{ | ||
params["params"].(map[string]interface{})["appName"].(string), | ||
params["params"].(map[string]interface{})["namespace"].(string), | ||
} | ||
} | ||
|
||
func parseComp(params map[string]interface{}) CompParams { | ||
tmp := params["params"].(map[string]interface{})["components"].([]interface{}) | ||
var components CompParams | ||
for _, compToParse := range tmp { | ||
component := compToParse.(map[string]interface{}) | ||
appParsed := AppParams{component["app"].(map[string]interface{})["appName"].(string), | ||
component["app"].(map[string]interface{})["namespace"].(string)} | ||
compParsed := Comp{appParsed, component["repo"].(string), component["compName"].(string)} | ||
components.components = append(components.components, compParsed) | ||
} | ||
return components | ||
} |
This file was deleted.
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.
Please use eventually, bad practice to use sleep in tests