| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243 |
- package kubernetes_test
- import (
- "reflect"
- "testing"
- "github.com/porter-dev/porter/internal/kubernetes"
- "github.com/porter-dev/porter/internal/models"
- "gopkg.in/yaml.v2"
- )
- type KubeConfigTest struct {
- msg string
- raw []byte
- allowedClusters []string
- expected []models.ClusterConfig
- }
- var MissingFieldsTest = []KubeConfigTest{
- KubeConfigTest{
- msg: "no fields at all",
- raw: []byte(""),
- allowedClusters: []string{},
- expected: []models.ClusterConfig{},
- },
- KubeConfigTest{
- msg: "no contexts to join",
- raw: []byte(noContexts),
- allowedClusters: []string{},
- expected: []models.ClusterConfig{},
- },
- KubeConfigTest{
- msg: "no clusters to join",
- raw: []byte(noClusters),
- allowedClusters: []string{},
- expected: []models.ClusterConfig{},
- },
- KubeConfigTest{
- msg: "no users to join",
- raw: []byte(noUsers),
- allowedClusters: []string{},
- expected: []models.ClusterConfig{},
- },
- KubeConfigTest{
- msg: "no cluster contexts to join",
- raw: []byte(noContextClusters),
- allowedClusters: []string{},
- expected: []models.ClusterConfig{},
- },
- KubeConfigTest{
- msg: "no cluster users to join",
- raw: []byte(noContextUsers),
- allowedClusters: []string{},
- expected: []models.ClusterConfig{},
- },
- }
- func TestToClusterConfigsMissingFields(t *testing.T) {
- for _, c := range MissingFieldsTest {
- // take raw and decode
- conf := kubernetes.KubeConfig{}
- err := yaml.Unmarshal(c.raw, &conf)
- if err != nil {
- t.Errorf("Testing: %s, Error: %v\n", c.msg, err)
- }
- res := conf.ToAllowedClusterConfigs(c.allowedClusters)
- isEqual := reflect.DeepEqual(c.expected, res)
- if !isEqual {
- t.Errorf("Testing: %s, Expected: %v, Got: %v\n", c.msg, c.expected, res)
- }
- }
- }
- var NoAllowedClustersTests = []KubeConfigTest{
- KubeConfigTest{
- msg: "basic test",
- raw: []byte(basic),
- allowedClusters: []string{},
- expected: []models.ClusterConfig{},
- },
- }
- func TestToClusterConfigsNoAllowedClusters(t *testing.T) {
- for _, c := range NoAllowedClustersTests {
- // take raw and decode
- conf := kubernetes.KubeConfig{}
- err := yaml.Unmarshal(c.raw, &conf)
- if err != nil {
- t.Errorf("Testing: %s, Error: %v\n", c.msg, err)
- }
- res := conf.ToAllowedClusterConfigs(c.allowedClusters)
- isEqual := reflect.DeepEqual(c.expected, res)
- if !isEqual {
- t.Errorf("Testing: %s, Expected: %v, Got: %v\n", c.msg, c.expected, res)
- }
- }
- }
- var BasicClustersTests = []KubeConfigTest{
- KubeConfigTest{
- msg: "basic test",
- raw: []byte(basic),
- allowedClusters: []string{"cluster-test"},
- expected: []models.ClusterConfig{
- models.ClusterConfig{
- Name: "cluster-test",
- Server: "https://localhost",
- Context: "context-test",
- User: "test-admin",
- },
- },
- },
- }
- func TestToClusterConfigsBasic(t *testing.T) {
- for _, c := range BasicClustersTests {
- // take raw and decode
- conf := kubernetes.KubeConfig{}
- err := yaml.Unmarshal(c.raw, &conf)
- if err != nil {
- t.Errorf("Testing: %s, Error: %v\n", c.msg, err)
- }
- res := conf.ToAllowedClusterConfigs(c.allowedClusters)
- isEqual := reflect.DeepEqual(c.expected, res)
- if !isEqual {
- t.Errorf("Testing: %s, Expected: %v, Got: %v\n", c.msg, c.expected, res)
- }
- }
- }
- const noContexts string = `
- apiVersion: v1
- kind: Config
- preferences: {}
- clusters:
- - cluster:
- server: https://localhost
- name: porter-test-1
- current-context: default
- users:
- - name: test-admin
- user:
- `
- const noClusters string = `
- apiVersion: v1
- kind: Config
- preferences: {}
- current-context: default
- contexts:
- - context:
- cluster: porter-test-1
- user: test-admin
- name: context-test
- users:
- - name: test-admin
- user:
- `
- const noUsers string = `
- apiVersion: v1
- kind: Config
- preferences: {}
- current-context: default
- clusters:
- - cluster:
- server: https://localhost
- name: porter-test-1
- contexts:
- - context:
- cluster: porter-test-1
- user: test-admin
- name: context-test
- `
- const noContextClusters string = `
- apiVersion: v1
- kind: Config
- preferences: {}
- current-context: default
- clusters:
- - cluster:
- server: https://localhost
- name: porter-test-1
- contexts:
- - context:
- # cluster: porter-test-1
- user: test-admin
- name: context-test
- users:
- - name: test-admin
- user:
- `
- const noContextUsers string = `
- apiVersion: v1
- kind: Config
- preferences: {}
- current-context: default
- clusters:
- - cluster:
- server: https://localhost
- name: porter-test-1
- contexts:
- - context:
- cluster: porter-test-1
- # user: test-admin
- name: context-test
- users:
- - name: test-admin
- user:
- `
- const basic string = `
- apiVersion: v1
- kind: Config
- preferences: {}
- current-context: default
- clusters:
- - cluster:
- server: https://localhost
- name: cluster-test
- contexts:
- - context:
- cluster: cluster-test
- user: test-admin
- name: context-test
- users:
- - name: test-admin
- `
|