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

Ginkgo tests #17

Merged
merged 2 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion api/v1alpha1/maasvalidator_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ type MaasInstanceRules struct {
OSImages []OSImage `json:"bootable-images,omitempty" yaml:"bootable-images,omitempty"`

// Auth provides authentication information for the MaaS Instance
Auth Auth `json:"auth,omitempty" yaml:"auth,omitempty"`
Auth Auth `json:"auth" yaml:"auth"`
}

type OSImage struct {
Expand Down
1 change: 1 addition & 0 deletions chart/validator-plugin-maas/crds/maasvalidator-crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ spec:
description: Unique rule name
type: string
required:
- auth
- name
type: object
required:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ spec:
description: Unique rule name
type: string
required:
- auth
- name
type: object
required:
Expand Down
4 changes: 2 additions & 2 deletions internal/controller/maasvalidator_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,12 @@ func (r *MaasValidatorReconciler) Reconcile(ctx context.Context, req ctrl.Reques
maasUrl := validator.Spec.MaasInstance.Host
maasclient, err := maasclient.GetClient(maasUrl, maasToken, "2.0")

apiclient := val.MaaSAPI{Client: maasclient}

if err != nil {
r.Log.Error(err, "failed to initialize MaaS client")
}

apiclient := val.MaaSAPI{Client: maasclient}

// Get the active validator's validation result
vr := &vapi.ValidationResult{}
nn := ktypes.NamespacedName{
Expand Down
52 changes: 52 additions & 0 deletions internal/controller/maasvalidator_controller_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
package controller

import (
"context"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"

// . "github.com/onsi/gomega"
//+kubebuilder:scaffold:imports
"github.com/spectrocloud-labs/validator-plugin-maas/api/v1alpha1"
vapi "github.com/spectrocloud-labs/validator/api/v1alpha1"
)

const MaasValidatorName = "maas-validator"
Expand All @@ -16,4 +25,47 @@ var _ = Describe("MaaSValidator controller", Ordered, func() {
Skip("skipping")
}
})

val := &v1alpha1.MaasValidator{
ObjectMeta: metav1.ObjectMeta{
Name: MaasValidatorName,
Namespace: validatorNamespace,
},
Spec: v1alpha1.MaasValidatorSpec{
MaasInstance: v1alpha1.MaasInstance{
Host: "maas.sc",
Auth: v1alpha1.Auth{
SecretName: "maas-api-token",
},
},
MaasInstanceRules: v1alpha1.MaasInstanceRules{
Name: "validate ubuntu images",
OSImages: []v1alpha1.OSImage{
{Name: "Ubuntu", Architecture: "amd64/ga-20.04"},
},
},
},
}

//secret := &corev1.Secret{}

vr := &vapi.ValidationResult{}
vrKey := types.NamespacedName{Name: validationResultName(val), Namespace: validatorNamespace}

It("Should create a ValidationResult and update its Status with a failed condition", func() {
By("By creating a new MaasValidator")
ctx := context.Background()

Expect(k8sClient.Create(ctx, val)).Should(Succeed())

// Wait for the ValidationResult's Status to be updated
Eventually(func() bool {
if err := k8sClient.Get(ctx, vrKey, vr); err != nil {
return false
}

stateOk := vr.Status.State == vapi.ValidationFailed
return stateOk
}, timeout, interval).Should(BeTrue(), "failed to create a ValidationResult")
})
})
20 changes: 16 additions & 4 deletions internal/validators/maas_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,25 @@
}

func (m *MaaSAPI) ListOSImages() ([]entity.BootResource, error) {
images, _ := m.Client.BootResources.Get(&entity.BootResourcesReadParams{})
return images, nil
if m.Client != nil {
images, err := m.Client.BootResources.Get(&entity.BootResourcesReadParams{})
if err != nil {
return make([]entity.BootResource, 0), err

Check warning on line 38 in internal/validators/maas_validator.go

View check run for this annotation

Codecov / codecov/patch

internal/validators/maas_validator.go#L35-L38

Added lines #L35 - L38 were not covered by tests
}
return images, nil

Check warning on line 40 in internal/validators/maas_validator.go

View check run for this annotation

Codecov / codecov/patch

internal/validators/maas_validator.go#L40

Added line #L40 was not covered by tests
}
return make([]entity.BootResource, 0), nil

Check warning on line 42 in internal/validators/maas_validator.go

View check run for this annotation

Codecov / codecov/patch

internal/validators/maas_validator.go#L42

Added line #L42 was not covered by tests
}

func (m *MaaSAPI) ListDNSServers() ([]entity.DNSResource, error) {
dnsresources, _ := m.Client.DNSResources.Get()
return dnsresources, nil
if m.Client != nil {
dnsresources, err := m.Client.DNSResources.Get()
if err != nil {
return make([]entity.DNSResource, 0), err

Check warning on line 49 in internal/validators/maas_validator.go

View check run for this annotation

Codecov / codecov/patch

internal/validators/maas_validator.go#L46-L49

Added lines #L46 - L49 were not covered by tests
}
return dnsresources, nil

Check warning on line 51 in internal/validators/maas_validator.go

View check run for this annotation

Codecov / codecov/patch

internal/validators/maas_validator.go#L51

Added line #L51 was not covered by tests
}
return make([]entity.DNSResource, 0), nil

Check warning on line 53 in internal/validators/maas_validator.go

View check run for this annotation

Codecov / codecov/patch

internal/validators/maas_validator.go#L53

Added line #L53 was not covered by tests
}

func NewMaasRuleService(apiclient MaaSAPIClient) *MaasRuleService {
Expand Down
Loading