github.com/caos/orbos@v1.5.14-0.20221103111702-e6cd0cea7ad4/pkg/kubernetes/resources/ambassador/host/adapt_test.go (about)

     1  package host
     2  
     3  import (
     4  	"testing"
     5  
     6  	"gopkg.in/yaml.v3"
     7  )
     8  
     9  func Test_contains(t *testing.T) {
    10  
    11  	unmarshal := func(from string) map[string]interface{} {
    12  		to := make(map[string]interface{})
    13  		if err := yaml.Unmarshal([]byte(from), to); err != nil {
    14  			t.Fatalf("unmarshalling failed: %v: %s", err, from)
    15  		}
    16  		return to
    17  	}
    18  
    19  	tests := []struct {
    20  		name   string
    21  		subset string
    22  		want   bool
    23  	}{{
    24  		name: "it should contain",
    25  		subset: `
    26  apiVersion: getambassador.io/v2
    27  kind: Host
    28  metadata:
    29    annotations:
    30      aes_res_changed: "true"
    31    labels:
    32      caos.ch/apiversion: v0
    33      caos.ch/kind: ZITADEL
    34    name: accounts
    35    namespace: caos-zitadel
    36  spec:
    37    acmeProvider:
    38      authority: https://acme-v02.api.letsencrypt.org/directory
    39    ambassador_id:
    40    - default
    41    hostname: accounts.iam.sustema.ai
    42    selector:
    43      matchLabels:
    44        hostname: accounts.iam.sustema.ai
    45  `,
    46  		want: true,
    47  	}, {
    48  		name: "it should not contain",
    49  		subset: `
    50  apiVersion: getambassador.io/v2
    51  kind: Host
    52  metadata:
    53    annotations:
    54      aes_res_changed: "true"
    55    labels:
    56      caos.ch/apiversion: v0
    57      caos.ch/kind: ZITADEL
    58    name: accounts
    59    namespace: caos-zitadel
    60  spec:
    61    acmeProvider:
    62      authority: https://acme-v02.api.letsencrypt.org/directory
    63    ambassador_id:
    64    - default
    65    - blubb ####################################################################
    66    hostname: accounts.iam.sustema.ai
    67    selector:
    68      matchLabels:
    69        hostname: accounts.iam.sustema.ai
    70    tlsSecret:
    71      name: accounts.iam.sustema.ai
    72  `,
    73  		want: false,
    74  	}}
    75  
    76  	existing := unmarshal(`
    77  apiVersion: getambassador.io/v2
    78  kind: Host
    79  metadata:
    80    annotations:
    81      aes_res_changed: "true"
    82    creationTimestamp: "2021-04-21T15:04:55Z"
    83    generation: 9
    84    labels:
    85      caos.ch/apiversion: v0
    86      caos.ch/kind: ZITADEL
    87    managedFields:
    88    - apiVersion: getambassador.io/v2
    89      fieldsType: FieldsV1
    90      fieldsV1:
    91        f:metadata:
    92          f:annotations:
    93            .: {}
    94            f:aes_res_changed: {}
    95          f:labels:
    96            .: {}
    97            f:app.kubernetes.io/component: {}
    98      manager: zitadelctl
    99    name: accounts
   100    namespace: caos-zitadel
   101    resourceVersion: "487823"
   102    selfLink: /apis/getambassador.io/v2/namespaces/caos-zitadel/hosts/accounts
   103    uid: ffa02ea0-6b40-43d9-abd8-53e135e98e6a
   104  spec:
   105    acmeProvider:
   106      authority: https://acme-v02.api.letsencrypt.org/directory
   107      privateKeySecret:
   108        name: https-3a-2f-2facme-2dv02.api.letsencrypt.org-2fdirectory
   109      registration: '{"body":{"status":"valid"},"uri":"https://acme-v02.api.letsencrypt.org/acme/acct/120159215"}'
   110    ambassador_id:
   111    - default
   112    hostname: accounts.iam.sustema.ai
   113    selector:
   114      matchLabels:
   115        hostname: accounts.iam.sustema.ai
   116    tlsSecret:
   117      name: accounts.iam.sustema.ai
   118  status:
   119    state: Ready
   120    tlsCertificateSource: ACME
   121  `)
   122  
   123  	for _, tt := range tests {
   124  		t.Run(tt.name, func(t *testing.T) {
   125  
   126  			if got := contains(existing, unmarshal(tt.subset)); got != tt.want {
   127  				t.Errorf("contains() = %v, want %v", got, tt.want)
   128  			}
   129  		})
   130  	}
   131  }