github.com/freiheit-com/kuberpult@v1.24.2-0.20240328135542-315d5630abe6/services/cd-service/pkg/service/environment_test.go (about)

     1  /*This file is part of kuberpult.
     2  
     3  Kuberpult is free software: you can redistribute it and/or modify
     4  it under the terms of the Expat(MIT) License as published by
     5  the Free Software Foundation.
     6  
     7  Kuberpult is distributed in the hope that it will be useful,
     8  but WITHOUT ANY WARRANTY; without even the implied warranty of
     9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    10  MIT License for more details.
    11  
    12  You should have received a copy of the MIT License
    13  along with kuberpult. If not, see <https://directory.fsf.org/wiki/License:Expat>.
    14  
    15  Copyright 2023 freiheit.com*/
    16  
    17  package service
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/freiheit-com/kuberpult/pkg/api/v1"
    23  	"github.com/freiheit-com/kuberpult/pkg/ptr"
    24  	"github.com/freiheit-com/kuberpult/services/cd-service/pkg/config"
    25  	"github.com/google/go-cmp/cmp"
    26  	"github.com/google/go-cmp/cmp/cmpopts"
    27  )
    28  
    29  func TestEnnvironmentConfigToApi(t *testing.T) {
    30  	tcs := []struct {
    31  		Name              string
    32  		configConfig      config.EnvironmentConfig
    33  		expectedApiConfig api.EnvironmentConfig
    34  	}{
    35  		{
    36  			Name: "basic tranformation to api",
    37  			configConfig: config.EnvironmentConfig{
    38  				Upstream: &config.EnvironmentConfigUpstream{
    39  					Environment: "upstream",
    40  					Latest:      true,
    41  				},
    42  				ArgoCd: &config.EnvironmentConfigArgoCd{
    43  					Destination: config.ArgoCdDestination{
    44  						Name:                 "destination",
    45  						Server:               "destinationServer",
    46  						Namespace:            ptr.FromString("destinationNamespace"),
    47  						AppProjectNamespace:  ptr.FromString("destinationAppProjectNamespace"),
    48  						ApplicationNamespace: ptr.FromString("destinationApplicationNamespace"),
    49  					},
    50  					SyncWindows: []config.ArgoCdSyncWindow{
    51  						{
    52  							Schedule: "syncWindowSchedule",
    53  							Duration: "syncWindowDuration",
    54  							Kind:     "syncWindowKind",
    55  							Apps: []string{
    56  								"app1",
    57  								"app2",
    58  								"app3",
    59  							},
    60  						},
    61  					},
    62  					ClusterResourceWhitelist: []config.AccessEntry{
    63  						{
    64  							Group: "accessListGroup",
    65  							Kind:  "accessListKind",
    66  						},
    67  					},
    68  					ApplicationAnnotations: map[string]string{
    69  						"app1": "foo",
    70  					},
    71  					IgnoreDifferences: []config.ArgoCdIgnoreDifference{
    72  						{
    73  							Group:     "diffGroup",
    74  							Kind:      "diffKind",
    75  							Name:      "diffName",
    76  							Namespace: "diffNamespace",
    77  							JSONPointers: []string{
    78  								"diffJSONPointer",
    79  							},
    80  							JqPathExpressions: []string{
    81  								"diffJqPathExpression",
    82  							},
    83  							ManagedFieldsManagers: []string{
    84  								"managedFieldsManager",
    85  							},
    86  						},
    87  					},
    88  					SyncOptions: []string{
    89  						"syncOption",
    90  					},
    91  				},
    92  			},
    93  			expectedApiConfig: api.EnvironmentConfig{
    94  				Upstream: &api.EnvironmentConfig_Upstream{
    95  					Environment: ptr.FromString("upstream"),
    96  					Latest:      ptr.Bool(true),
    97  				},
    98  				Argocd: &api.EnvironmentConfig_ArgoCD{
    99  					SyncWindows: []*api.EnvironmentConfig_ArgoCD_SyncWindows{
   100  						{
   101  							Kind:     "syncWindowKind",
   102  							Schedule: "syncWindowSchedule",
   103  							Duration: "syncWindowDuration",
   104  						},
   105  					},
   106  					Destination: &api.EnvironmentConfig_ArgoCD_Destination{
   107  						Name:                 "destination",
   108  						Server:               "destinationServer",
   109  						Namespace:            ptr.FromString("destinationNamespace"),
   110  						AppProjectNamespace:  ptr.FromString("destinationAppProjectNamespace"),
   111  						ApplicationNamespace: ptr.FromString("destinationApplicationNamespace"),
   112  					},
   113  					AccessList: []*api.EnvironmentConfig_ArgoCD_AccessEntry{
   114  						{
   115  							Group: "accessListGroup",
   116  							Kind:  "accessListKind",
   117  						},
   118  					},
   119  				},
   120  				EnvironmentGroup: nil,
   121  			},
   122  		},
   123  	}
   124  	for _, tc := range tcs {
   125  		tc := tc
   126  		t.Run(tc.Name, func(t *testing.T) {
   127  			actualApiConfig := TransformEnvironmentConfigToApi(tc.configConfig)
   128  			// first diff individual parts as they ensure shorter readable diffs ...
   129  			if !cmp.Equal(tc.expectedApiConfig.Upstream, actualApiConfig.Upstream, cmpopts.IgnoreUnexported(api.EnvironmentConfig_Upstream{})) {
   130  				t.Fatalf("transformed api config upstream does not match expectation: %s", cmp.Diff(tc.expectedApiConfig.Upstream, actualApiConfig.Upstream, cmpopts.IgnoreUnexported(api.EnvironmentConfig_Upstream{})))
   131  			}
   132  			if !cmp.Equal(tc.expectedApiConfig.Argocd, actualApiConfig.Argocd, cmpopts.IgnoreUnexported(
   133  				api.EnvironmentConfig_ArgoCD{},
   134  				api.EnvironmentConfig_ArgoCD_AccessEntry{},
   135  				api.EnvironmentConfig_ArgoCD_Destination{},
   136  				api.EnvironmentConfig_ArgoCD_IgnoreDifferences{},
   137  				api.EnvironmentConfig_ArgoCD_SyncWindows{},
   138  			)) {
   139  				t.Fatalf("transformed api config argo cd does not match expectation: %s", cmp.Diff(tc.expectedApiConfig.Argocd, actualApiConfig.Argocd, cmpopts.IgnoreUnexported(
   140  					api.EnvironmentConfig_ArgoCD{},
   141  					api.EnvironmentConfig_ArgoCD_AccessEntry{},
   142  					api.EnvironmentConfig_ArgoCD_Destination{},
   143  					api.EnvironmentConfig_ArgoCD_IgnoreDifferences{},
   144  					api.EnvironmentConfig_ArgoCD_SyncWindows{},
   145  				)))
   146  			}
   147  			if !cmp.Equal(tc.expectedApiConfig.EnvironmentGroup, actualApiConfig.EnvironmentGroup, cmpopts.IgnoreUnexported(api.EnvironmentGroup{})) {
   148  				t.Fatalf("transformed api config env group does not match expectation: %s", cmp.Diff(tc.expectedApiConfig.EnvironmentGroup, actualApiConfig.EnvironmentGroup, cmpopts.IgnoreUnexported(api.EnvironmentGroup{})))
   149  			}
   150  			// ... then compare the full struct.
   151  			if !cmp.Equal(&tc.expectedApiConfig, actualApiConfig, cmpopts.IgnoreUnexported(
   152  				api.EnvironmentConfig{},
   153  				api.EnvironmentConfig_Upstream{},
   154  				api.EnvironmentConfig_ArgoCD{},
   155  				api.EnvironmentConfig_ArgoCD_AccessEntry{},
   156  				api.EnvironmentConfig_ArgoCD_Destination{},
   157  				api.EnvironmentConfig_ArgoCD_IgnoreDifferences{},
   158  				api.EnvironmentConfig_ArgoCD_SyncWindows{},
   159  			)) {
   160  				t.Fatalf("transformed api config does not match expectation: %s", cmp.Diff(&tc.expectedApiConfig, actualApiConfig, cmpopts.IgnoreUnexported(
   161  					api.EnvironmentConfig{},
   162  					api.EnvironmentConfig_Upstream{},
   163  					api.EnvironmentConfig_ArgoCD{},
   164  					api.EnvironmentConfig_ArgoCD_AccessEntry{},
   165  					api.EnvironmentConfig_ArgoCD_Destination{},
   166  					api.EnvironmentConfig_ArgoCD_IgnoreDifferences{},
   167  					api.EnvironmentConfig_ArgoCD_SyncWindows{},
   168  				)))
   169  			}
   170  		})
   171  	}
   172  }