mirror of
https://github.com/FairwindsOps/polaris.git
synced 2026-08-21 13:16:51 +00:00
* Start working on updating dependencies: * Fix webhook * Rollback jsonschema update * Checkin new config * Fix run as root * Update versions of kind * Fix typo in kind URL * Fix kind config * Add csr permissions * Fix weird image thing * Fixed certificates * Add to logging * Approve cert manually * Fix approval * Add cert script * Fix deployment * Add requests/limits * Wait if certificate doesn't exist yet * Add check for file size * Add variable * Try a different imagE * Fix command * Update certificate logic * Add healthz * Don't check cert size * Remove stat * Fix vet * Put in change that makes no sense * Fix cert names * Roll back * Try changing config * Add logging for each request * Cleanup code some * Remove bad deployments * Fix client injection * Update timeout * Add logging * Fixed e2e webhook tests * Add permissions for approval * Fix permissions for CSR * Remove logging code * Remove refresh certs file * Fix merge issues * Update deployments * Try beta of admission controller config * Target 1.15 for testing * Add beta versions of resourceS * Lower webhook timeout * Refactor out a method * Fix up PR issues * Fix more tabs * Remove unnecessary messageS * Fix go.sum * Fix go.sum
106 lines
4.2 KiB
Go
106 lines
4.2 KiB
Go
package kube
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"io/ioutil"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/fairwindsops/polaris/test"
|
|
"github.com/stretchr/testify/assert"
|
|
corev1 "k8s.io/api/core/v1"
|
|
)
|
|
|
|
func TestGetResourcesFromPath(t *testing.T) {
|
|
resources, err := CreateResourceProviderFromPath("./test_files/test_1")
|
|
|
|
assert.Equal(t, nil, err, "Error should be nil")
|
|
|
|
assert.Equal(t, "Path", resources.SourceType, "Should have type Path")
|
|
assert.Equal(t, "./test_files/test_1", resources.SourceName, "Should have filename as name")
|
|
assert.Equal(t, "unknown", resources.ServerVersion, "Server version should be unknown")
|
|
assert.IsType(t, time.Now(), resources.CreationTime, "Creation time should be set")
|
|
|
|
assert.Equal(t, 0, len(resources.Nodes), "Should not have any nodes")
|
|
|
|
assert.Equal(t, 1, len(resources.Namespaces), "Should have a namespace")
|
|
assert.Equal(t, "two", resources.Namespaces[0].ObjectMeta.Name)
|
|
|
|
assert.Equal(t, 8, len(resources.Controllers), "Should have eight controllers")
|
|
namespaceCount := map[string]int{}
|
|
for _, controller := range resources.Controllers {
|
|
namespaceCount[controller.ObjectMeta.GetNamespace()]++
|
|
}
|
|
assert.Equal(t, 7, namespaceCount[""], "Should have seven controller in default namespace")
|
|
assert.Equal(t, 1, namespaceCount["two"], "Should have one controller in namespace 'two'")
|
|
}
|
|
|
|
func TestGetMultipleResourceFromSingleFile(t *testing.T) {
|
|
resources, err := CreateResourceProviderFromPath("./test_files/test_2/multi.yaml")
|
|
|
|
assert.Equal(t, nil, err, "Error should be nil")
|
|
|
|
assert.Equal(t, "Path", resources.SourceType, "Should have type Path")
|
|
assert.Equal(t, "./test_files/test_2/multi.yaml", resources.SourceName, "Should have filename as name")
|
|
assert.Equal(t, "unknown", resources.ServerVersion, "Server version should be unknown")
|
|
assert.IsType(t, time.Now(), resources.CreationTime, "Creation time should be set")
|
|
|
|
assert.Equal(t, 0, len(resources.Nodes), "Should not have any nodes")
|
|
|
|
assert.Equal(t, 1, len(resources.Controllers), "Should have one controller")
|
|
assert.Equal(t, "dashboard", resources.Controllers[0].PodSpec.Containers[0].Name)
|
|
|
|
assert.Equal(t, 2, len(resources.Namespaces), "Should have a namespace")
|
|
assert.Equal(t, "polaris", resources.Namespaces[0].ObjectMeta.Name)
|
|
assert.Equal(t, "polaris-2", resources.Namespaces[1].ObjectMeta.Name)
|
|
}
|
|
|
|
func TestGetMultipleResourceFromBadFile(t *testing.T) {
|
|
_, err := CreateResourceProviderFromPath("./test_files/test_3")
|
|
assert.NotEqual(t, nil, err, "CreateResource From Path should fail with bad yaml")
|
|
}
|
|
|
|
func TestAddResourcesFromReader(t *testing.T) {
|
|
contents, err := ioutil.ReadFile("./test_files/test_2/multi.yaml")
|
|
assert.NoError(t, err)
|
|
reader := bytes.NewBuffer(contents)
|
|
resources := &ResourceProvider{
|
|
ServerVersion: "unknown",
|
|
SourceType: "Path",
|
|
SourceName: "-",
|
|
Nodes: []corev1.Node{},
|
|
Namespaces: []corev1.Namespace{},
|
|
Controllers: []GenericWorkload{},
|
|
}
|
|
err = addResourcesFromReader(reader, resources)
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 0, len(resources.Nodes), "Should not have any nodes")
|
|
|
|
assert.Equal(t, 1, len(resources.Controllers), "Should have one controller")
|
|
assert.Equal(t, "dashboard", resources.Controllers[0].PodSpec.Containers[0].Name)
|
|
|
|
assert.Equal(t, 2, len(resources.Namespaces), "Should have a namespace")
|
|
assert.Equal(t, "polaris", resources.Namespaces[0].ObjectMeta.Name)
|
|
assert.Equal(t, "polaris-2", resources.Namespaces[1].ObjectMeta.Name)
|
|
}
|
|
|
|
func TestGetResourceFromAPI(t *testing.T) {
|
|
k8s, dynamicInterface := test.SetupTestAPI()
|
|
k8s = test.SetupAddControllers(context.Background(), k8s, "test")
|
|
// TODO find a way to mock out the dynamic client
|
|
// and create fake pods in order to find all of the controllers.
|
|
resources, err := CreateResourceProviderFromAPI(context.Background(), k8s, "test", &dynamicInterface)
|
|
assert.Equal(t, nil, err, "Error should be nil")
|
|
|
|
assert.Equal(t, "Cluster", resources.SourceType, "Should have type Path")
|
|
assert.Equal(t, "test", resources.SourceName, "Should have source name")
|
|
assert.IsType(t, time.Now(), resources.CreationTime, "Creation time should be set")
|
|
|
|
assert.Equal(t, 0, len(resources.Nodes), "Should not have any nodes")
|
|
assert.Equal(t, 1, len(resources.Controllers), "Should have 1 controller")
|
|
|
|
assert.Equal(t, "", resources.Controllers[0].ObjectMeta.GetName())
|
|
}
|