github.com/Mirantis/virtlet@v1.5.2-0.20191204181327-1659b8a48e9b/pkg/imagetranslation/crd_source_test.go (about)

     1  /*
     2  Copyright 2018 Mirantis
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package imagetranslation
    18  
    19  import (
    20  	"context"
    21  	"reflect"
    22  	"strings"
    23  	"testing"
    24  
    25  	"github.com/ghodss/yaml"
    26  	meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    27  
    28  	virtlet_v1 "github.com/Mirantis/virtlet/pkg/api/virtlet.k8s/v1"
    29  	"github.com/Mirantis/virtlet/pkg/client/clientset/versioned/fake"
    30  )
    31  
    32  func TestCRDConfigSource(t *testing.T) {
    33  	srcConfigs := []*virtlet_v1.VirtletImageMapping{
    34  		{
    35  			ObjectMeta: meta_v1.ObjectMeta{
    36  				Name:      "mapping1",
    37  				Namespace: "foobar",
    38  			},
    39  			Spec: virtlet_v1.ImageTranslation{
    40  				Rules: []virtlet_v1.TranslationRule{
    41  					{
    42  						Name: "testimage1",
    43  						URL:  "https://example.com/testimage1.qcow2",
    44  					},
    45  				},
    46  			},
    47  		},
    48  		{
    49  			ObjectMeta: meta_v1.ObjectMeta{
    50  				Name:      "mapping2",
    51  				Namespace: "foobar",
    52  			},
    53  			Spec: virtlet_v1.ImageTranslation{
    54  				Rules: []virtlet_v1.TranslationRule{
    55  					{
    56  						Name: "testimage2",
    57  						URL:  "https://example.com/testimage2.qcow2",
    58  					},
    59  				},
    60  			},
    61  		},
    62  		{
    63  			ObjectMeta: meta_v1.ObjectMeta{
    64  				Name:      "mapping3_skipme",
    65  				Namespace: "skipthisns",
    66  			},
    67  			Spec: virtlet_v1.ImageTranslation{
    68  				Rules: []virtlet_v1.TranslationRule{
    69  					{
    70  						Name: "testimage3_skipme",
    71  						URL:  "https://example.com/nosuchimage.qcow2",
    72  					},
    73  				},
    74  			},
    75  		},
    76  	}
    77  	clientset := fake.NewSimpleClientset(srcConfigs[0], srcConfigs[1], srcConfigs[2])
    78  	cs := NewCRDSource("foobar", nil)
    79  	cs.(*crdConfigSource).virtletClient = clientset
    80  
    81  	desc := cs.Description()
    82  	if !strings.Contains(desc, "foobar") {
    83  		t.Errorf("Namespace name 'foobar' not in CRD source description: %q", desc)
    84  	}
    85  
    86  	expectedConfigs := []TranslationConfig{srcConfigs[0], srcConfigs[1]}
    87  	configs, err := cs.Configs(context.Background())
    88  	if err != nil {
    89  		t.Fatalf("Configs(): %v", err)
    90  	}
    91  	if !reflect.DeepEqual(expectedConfigs, configs) {
    92  		expected, err := yaml.Marshal(expectedConfigs)
    93  		if err != nil {
    94  			t.Errorf("Error marshalling yaml: %v", err)
    95  		}
    96  		actual, err := yaml.Marshal(configs)
    97  		if err != nil {
    98  			t.Errorf("Error marshalling yaml: %v", err)
    99  		}
   100  		t.Errorf("Bad config list.\n--- expected configs ---\n%s\n--- actual configs ---\n%s", expected, actual)
   101  	}
   102  }