Skip to content

Commit

Permalink
unit-test: add register tests
Browse files Browse the repository at this point in the history
Signed-off-by: Francesco Giudici <[email protected]>
  • Loading branch information
fgiudici committed Mar 27, 2024
1 parent 1f13da6 commit c6aac92
Show file tree
Hide file tree
Showing 2 changed files with 150 additions and 0 deletions.
69 changes: 69 additions & 0 deletions pkg/plainauth/register_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
Copyright © 2022 - 2024 SUSE LLC
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 plainauth

import (
"testing"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
elementalv1 "github.com/rancher/elemental-operator/api/v1beta1"
)

func TestRegisterPlainAuth(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Register PlainAuth")
}

var _ = Describe("is not filled", Label("plainauth"), func() {
It("returns true when empty", func() {
value := ""
res := isNotFilled(value)
Expect(res).To(BeTrue())
})
It("returns true when 'Unspecified'", func() {
value := "Unspecified"
res := isNotFilled(value)
Expect(res).To(BeTrue())
})
It("returns true when 'None'", func() {
value := "None"
res := isNotFilled(value)
Expect(res).To(BeTrue())
})
It("returns true when 'Not ...'", func() {
value := "Not Present"
res := isNotFilled(value)
Expect(res).To(BeTrue())
})
It("returns false when has a regular value", func() {
value := "Data"
res := isNotFilled(value)
Expect(res).To(BeFalse())
})
})

var _ = Describe("authentication client init", Label("plainauth"), func() {
It("returns error on unknown registration authentication", func() {
reg := elementalv1.Registration{
Auth: "unknown",
}
authCli := AuthClient{}
err := authCli.Init(reg)
Expect(err).To(HaveOccurred())
})
})
81 changes: 81 additions & 0 deletions pkg/register/register_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
Copyright © 2022 - 2024 SUSE LLC
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 register

import (
"testing"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
elementalv1 "github.com/rancher/elemental-operator/api/v1beta1"
)

func TestRegisterUtils(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Register Core")
}

var _ = Describe("authenticator", Label("registration", "core"), func() {
It("returns TPM authentication on auth=tpm registration", func() {
reg := elementalv1.Registration{
Auth: "tpm",
}
state := &State{}

auth, err := getAuthenticator(reg, state)
Expect(err).ToNot(HaveOccurred())
Expect(auth.GetName()).To(Equal("TPM"))
Expect(state.EmulatedTPM).To(BeFalse())
})

It("fills State with TPM emulation data from registration", func() {
reg := elementalv1.Registration{
Auth: "tpm",
EmulateTPM: true,
EmulatedTPMSeed: 10,
}
state := &State{}

auth, err := getAuthenticator(reg, state)
Expect(err).ToNot(HaveOccurred())
Expect(auth.GetName()).To(Equal("TPM"))
Expect(state.EmulatedTPM).To(BeTrue())
Expect(state.EmulatedTPMSeed).To(Equal(int64(10)))
})

It("returns plain authentication on auth=mac registration", func() {
reg := elementalv1.Registration{
Auth: "mac",
}
state := &State{}

auth, err := getAuthenticator(reg, state)
Expect(err).ToNot(HaveOccurred())
Expect(auth.GetName()).To(Equal("Plain"))
})

It("returns plain authentication on auth=sys-uuid registration", func() {
reg := elementalv1.Registration{
Auth: "sys-uuid",
}
state := &State{}

auth, err := getAuthenticator(reg, state)
Expect(err).ToNot(HaveOccurred())
Expect(auth.GetName()).To(Equal("Plain"))
})
})

0 comments on commit c6aac92

Please sign in to comment.