Skip to content

Commit

Permalink
Merge pull request #60 from srl-labs/tests/util-tests
Browse files Browse the repository at this point in the history
tests: simple tests for util funcs
  • Loading branch information
carlmontanari authored Oct 21, 2023
2 parents 9a67243 + 89a3536 commit 0cbd254
Show file tree
Hide file tree
Showing 20 changed files with 744 additions and 63 deletions.
4 changes: 2 additions & 2 deletions clicker/clabernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -396,8 +396,8 @@ func (c *clabernetes) buildPods(
ImagePullPolicy: selfPod.Spec.Containers[0].ImagePullPolicy,
SecurityContext: &k8scorev1.SecurityContext{
// we need privileged for setting syscalls and such
Privileged: clabernetesutil.BoolToPointer(true),
RunAsUser: clabernetesutil.Int64ToPointer(0),
Privileged: clabernetesutil.ToPointer(true),
RunAsUser: clabernetesutil.ToPointer(int64(0)),
},
Env: []k8scorev1.EnvVar{
{
Expand Down
14 changes: 8 additions & 6 deletions controllers/topology/configmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const renderConfigMapTestName = "configmap/render-config-map"
// TestRenderConfigMap ensures that we properly render the main tunnel/config configmap for a given
// c9s deployment (containerlab CR).
func TestRenderConfigMap(t *testing.T) {
testCases := []struct {
cases := []struct {
name string
obj ctrlruntimeclient.Object
clabernetesConfigs map[string]*clabernetesutilcontainerlab.Config
Expand All @@ -59,7 +59,7 @@ func TestRenderConfigMap(t *testing.T) {
clabernetesConfigs: map[string]*clabernetesutilcontainerlab.Config{
"srl1": {
Name: "clabernetes-srl1",
Prefix: clabernetesutil.StringToPointer(""),
Prefix: clabernetesutil.ToPointer(""),
Topology: &clabernetesutilcontainerlab.Topology{
Defaults: &clabernetesutilcontainerlab.NodeDefinition{
Ports: defaultPorts,
Expand All @@ -84,7 +84,7 @@ func TestRenderConfigMap(t *testing.T) {
},
"srl2": {
Name: "clabernetes-srl2",
Prefix: clabernetesutil.StringToPointer(""),
Prefix: clabernetesutil.ToPointer(""),
Topology: &clabernetesutilcontainerlab.Topology{
Defaults: &clabernetesutilcontainerlab.NodeDefinition{
Ports: defaultPorts,
Expand Down Expand Up @@ -142,7 +142,7 @@ func TestRenderConfigMap(t *testing.T) {
clabernetesConfigs: map[string]*clabernetesutilcontainerlab.Config{
"srl1": {
Name: "clabernetes-srl1",
Prefix: clabernetesutil.StringToPointer(""),
Prefix: clabernetesutil.ToPointer(""),
Topology: &clabernetesutilcontainerlab.Topology{
Defaults: &clabernetesutilcontainerlab.NodeDefinition{
Ports: defaultPorts,
Expand All @@ -158,7 +158,7 @@ func TestRenderConfigMap(t *testing.T) {
},
"srl2": {
Name: "clabernetes-srl2",
Prefix: clabernetesutil.StringToPointer(""),
Prefix: clabernetesutil.ToPointer(""),
Topology: &clabernetesutilcontainerlab.Topology{
Defaults: &clabernetesutilcontainerlab.NodeDefinition{
Ports: defaultPorts,
Expand All @@ -180,10 +180,12 @@ func TestRenderConfigMap(t *testing.T) {
},
}

for _, testCase := range testCases {
for _, testCase := range cases {
t.Run(
testCase.name,
func(t *testing.T) {
t.Logf("%s: starting", testCase.name)

reconciler := clabernetescontrollerstopology.Reconciler{
ResourceKind: "containerlab",
ConfigManagerGetter: clabernetesconfig.GetFakeManager,
Expand Down
2 changes: 1 addition & 1 deletion controllers/topology/containerlab/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ func (c *Controller) processConfigForNode(
// what the user has provided *or* the default of "clab").
// since prefixes are only useful when multiple labs are scheduled on the same node, and
// that will never be the case with clabernetes, the prefix is unnecessary.
Prefix: clabernetesutil.StringToPointer(""),
Prefix: clabernetesutil.ToPointer(""),
}

for _, link := range clabTopo.Links {
Expand Down
8 changes: 4 additions & 4 deletions controllers/topology/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,8 +318,8 @@ func renderDeployment(
Labels: labels,
},
Spec: k8sappsv1.DeploymentSpec{
Replicas: clabernetesutil.Int32ToPointer(1),
RevisionHistoryLimit: clabernetesutil.Int32ToPointer(0),
Replicas: clabernetesutil.ToPointer(int32(1)),
RevisionHistoryLimit: clabernetesutil.ToPointer(int32(0)),
Selector: &metav1.LabelSelector{
MatchLabels: matchLabels,
},
Expand Down Expand Up @@ -369,8 +369,8 @@ func renderDeployment(
),
SecurityContext: &k8scorev1.SecurityContext{
// obviously we need privileged for dind setup
Privileged: clabernetesutil.BoolToPointer(true),
RunAsUser: clabernetesutil.Int64ToPointer(0),
Privileged: clabernetesutil.ToPointer(true),
RunAsUser: clabernetesutil.ToPointer(int64(0)),
},
Env: []k8scorev1.EnvVar{
{
Expand Down
2 changes: 1 addition & 1 deletion controllers/topology/kne/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func (c *Controller) processConfig( //nolint:funlen
},
Links: nil,
},
Prefix: clabernetesutil.StringToPointer(""),
Prefix: clabernetesutil.ToPointer(""),
}

if kneModel != "" {
Expand Down
5 changes: 4 additions & 1 deletion controllers/topology/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ import (
apimachinerytypes "k8s.io/apimachinery/pkg/types"
)

func serviceConforms(
// ServiceConforms asserts if a given service conforms with a rendered service -- this isn't
// checking if the services are exactly the same, just checking that the parts clabernetes cares
// about are the same.
func ServiceConforms(
existingService,
renderedService *k8scorev1.Service,
expectedOwnerUID apimachinerytypes.UID,
Expand Down
2 changes: 1 addition & 1 deletion controllers/topology/serviceexpose.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ func (r *Reconciler) enforceExposeServices(
return err
}

if !serviceConforms(service, expectedService, obj.GetUID()) {
if !ServiceConforms(service, expectedService, obj.GetUID()) {
r.Log.Debugf(
"comparing existing expose service '%s/%s' spec does not conform to desired "+
"state, updating",
Expand Down
2 changes: 1 addition & 1 deletion controllers/topology/servicefabric.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ func (r *Reconciler) enforceFabricServices(
return err
}

if !serviceConforms(service, expectedService, obj.GetUID()) {
if !ServiceConforms(service, expectedService, obj.GetUID()) {
r.Log.Debugf(
"comparing existing service '%s/%s' spec does not conform to desired state, "+
"updating",
Expand Down
4 changes: 2 additions & 2 deletions controllers/topology/tunnels_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const testAllocateTunnelIdsTestName = "tunnels/allocate-tunnel-ids"
// parts in play to ensure that we use the tunnel IDs consistently and also obviously don't stomp
// on any existing tunnel IDs.
func TestAllocateTunnelIds(t *testing.T) {
testCases := []struct {
cases := []struct {
name string
statusTunnels map[string][]*clabernetesapistopologyv1alpha1.Tunnel
processedTunnels map[string][]*clabernetesapistopologyv1alpha1.Tunnel
Expand Down Expand Up @@ -286,7 +286,7 @@ func TestAllocateTunnelIds(t *testing.T) {
},
}

for _, testCase := range testCases {
for _, testCase := range cases {
t.Run(
testCase.name,
func(t *testing.T) {
Expand Down
50 changes: 50 additions & 0 deletions util/bools_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package util_test

import (
"testing"

clabernetestesthelper "github.com/srl-labs/clabernetes/testhelper"
clabernetesutil "github.com/srl-labs/clabernetes/util"
)

func TestAnyBoolTrue(t *testing.T) {
cases := []struct {
name string
in []bool
expected bool
}{
{
name: "simple",
in: []bool{true},
expected: true,
},
{
name: "all-true",
in: []bool{true, true, true},
expected: true,
},
{
name: "one-true",
in: []bool{false, true, false},
expected: true,
},
{
name: "all-false",
in: []bool{false, false, false},
expected: false,
},
}

for _, testCase := range cases {
t.Run(
testCase.name,
func(t *testing.T) {
t.Logf("%s: starting", testCase.name)

actual := clabernetesutil.AnyBoolTrue(testCase.in...)
if actual != testCase.expected {
clabernetestesthelper.FailOutput(t, actual, testCase.expected)
}
})
}
}
33 changes: 0 additions & 33 deletions util/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@ package util

import (
"errors"
"io"
"io/fs"
"net/http"
"os"
"strings"
)

// MustCreateDirectory creates a directory at path `directory` with provided permissions, it panics
Expand All @@ -25,33 +22,3 @@ func MustFileExists(f string) bool {

return !errors.Is(err, os.ErrNotExist)
}

// ResolveAtFileOrURL returns the bytes from `path` where path is either a filepath or URL.
func ResolveAtFileOrURL(path string) ([]byte, error) {
var b []byte

switch {
case strings.HasPrefix(path, "http://") || strings.HasPrefix(path, "https://"):
resp, err := http.Get(path) //nolint:gosec,noctx
if err != nil {
return nil, err
}

defer resp.Body.Close() //nolint

b, err = io.ReadAll(resp.Body)
if err != nil {
return nil, err
}

default: // fall-through to local filesystem
var err error

b, err = os.ReadFile(path) //nolint:gosec
if err != nil {
return nil, err
}
}

return b, nil
}
35 changes: 35 additions & 0 deletions util/hash_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package util_test

import (
"testing"

clabernetestesthelper "github.com/srl-labs/clabernetes/testhelper"
clabernetesutil "github.com/srl-labs/clabernetes/util"
)

func TestHashBytes(t *testing.T) {
cases := []struct {
name string
in []byte
expected string
}{
{
name: "simple",
in: []byte("hashmeplz"),
expected: "b370f837831aa6b19d65cac4a0f8ef13e8d145027d3b95992232ccb8f0e564b5",
},
}

for _, testCase := range cases {
t.Run(
testCase.name,
func(t *testing.T) {
t.Logf("%s: starting", testCase.name)

actual := clabernetesutil.HashBytes(testCase.in)
if actual != testCase.expected {
clabernetestesthelper.FailOutput(t, actual, testCase.expected)
}
})
}
}
Loading

0 comments on commit 0cbd254

Please sign in to comment.