github.com/kubeshop/testkube@v1.17.23/internal/migrations/version_0.9.2.go (about)

     1  package migrations
     2  
     3  import (
     4  	"strings"
     5  
     6  	"k8s.io/apimachinery/pkg/api/errors"
     7  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
     8  
     9  	testsv1 "github.com/kubeshop/testkube-operator/api/tests/v1"
    10  	testsv3 "github.com/kubeshop/testkube-operator/api/tests/v3"
    11  	testsuite "github.com/kubeshop/testkube-operator/api/testsuite/v2"
    12  	scriptsclientv2 "github.com/kubeshop/testkube-operator/pkg/client/scripts/v2"
    13  	testsclientv1 "github.com/kubeshop/testkube-operator/pkg/client/tests"
    14  	testsclientv3 "github.com/kubeshop/testkube-operator/pkg/client/tests/v3"
    15  	testsuitesclientv2 "github.com/kubeshop/testkube-operator/pkg/client/testsuites/v2"
    16  	"github.com/kubeshop/testkube/pkg/api/v1/testkube"
    17  	"github.com/kubeshop/testkube/pkg/migrator"
    18  )
    19  
    20  func NewVersion_0_9_2(
    21  	scriptsClient *scriptsclientv2.ScriptsClient,
    22  	testsClientV1 *testsclientv1.TestsClient,
    23  	testsClientV3 *testsclientv3.TestsClient,
    24  	testsuitesClient *testsuitesclientv2.TestSuitesClient,
    25  ) *Version_0_9_2 {
    26  	return &Version_0_9_2{
    27  		scriptsClient:    scriptsClient,
    28  		testsClientV1:    testsClientV1,
    29  		testsClientV3:    testsClientV3,
    30  		testsuitesClient: testsuitesClient,
    31  	}
    32  }
    33  
    34  type Version_0_9_2 struct {
    35  	scriptsClient    *scriptsclientv2.ScriptsClient
    36  	testsClientV1    *testsclientv1.TestsClient
    37  	testsClientV3    *testsclientv3.TestsClient
    38  	testsuitesClient *testsuitesclientv2.TestSuitesClient
    39  }
    40  
    41  func (m *Version_0_9_2) Version() string {
    42  	return "0.9.2"
    43  }
    44  func (m *Version_0_9_2) Migrate() error {
    45  	scripts, err := m.scriptsClient.List(nil)
    46  	if err != nil {
    47  		return err
    48  	}
    49  
    50  	for _, script := range scripts.Items {
    51  		if _, err = m.testsClientV3.Get(script.Name); err != nil && !errors.IsNotFound(err) {
    52  			return err
    53  		}
    54  
    55  		if err == nil {
    56  			continue
    57  		}
    58  
    59  		test := &testsv3.Test{
    60  			ObjectMeta: metav1.ObjectMeta{
    61  				Name:      script.Name,
    62  				Namespace: script.Namespace,
    63  			},
    64  			Spec: testsv3.TestSpec{
    65  				Type_: script.Spec.Type_,
    66  				Name:  script.Spec.Name,
    67  			},
    68  		}
    69  
    70  		if len(script.Spec.Params) != 0 {
    71  			test.Spec.ExecutionRequest = &testsv3.ExecutionRequest{
    72  				Variables: make(map[string]testsv3.Variable, len(script.Spec.Params)),
    73  			}
    74  
    75  			for key, value := range script.Spec.Params {
    76  				test.Spec.ExecutionRequest.Variables[key] = testsv3.Variable{
    77  					Name:  key,
    78  					Value: value,
    79  					Type_: string(*testkube.VariableTypeBasic),
    80  				}
    81  			}
    82  		}
    83  
    84  		if script.Spec.Content != nil {
    85  			test.Spec.Content = &testsv3.TestContent{
    86  				Type_: testsv3.TestContentType(script.Spec.Content.Type_),
    87  				Data:  script.Spec.Content.Data,
    88  				Uri:   script.Spec.Content.Uri,
    89  			}
    90  
    91  			if script.Spec.Content.Repository != nil {
    92  				test.Spec.Content.Repository = &testsv3.Repository{
    93  					Type_:  script.Spec.Content.Repository.Type_,
    94  					Uri:    script.Spec.Content.Repository.Uri,
    95  					Branch: script.Spec.Content.Repository.Branch,
    96  					Path:   script.Spec.Content.Repository.Path,
    97  				}
    98  			}
    99  		}
   100  
   101  		if _, err = m.testsClientV3.Create(test, false); err != nil {
   102  			return err
   103  		}
   104  
   105  		if err = m.scriptsClient.Delete(script.Name); err != nil {
   106  			return err
   107  		}
   108  	}
   109  
   110  	tests, err := m.testsClientV1.List(nil)
   111  	if err != nil {
   112  		return err
   113  	}
   114  
   115  OUTER:
   116  	for _, test := range tests.Items {
   117  		if _, err = m.testsuitesClient.Get(test.Name); err != nil && !errors.IsNotFound(err) {
   118  			return err
   119  		}
   120  
   121  		if err == nil {
   122  			continue
   123  		}
   124  
   125  		for _, managedField := range test.GetManagedFields() {
   126  			if !strings.HasSuffix(managedField.APIVersion, "/v1") {
   127  				continue OUTER
   128  			}
   129  		}
   130  
   131  		testsuite := &testsuite.TestSuite{
   132  			ObjectMeta: metav1.ObjectMeta{
   133  				Name:      test.Name,
   134  				Namespace: test.Namespace,
   135  			},
   136  			Spec: testsuite.TestSuiteSpec{
   137  				Repeats:     test.Spec.Repeats,
   138  				Description: test.Spec.Description,
   139  			},
   140  		}
   141  
   142  		for _, step := range test.Spec.Before {
   143  			testsuite.Spec.Before = append(testsuite.Spec.Before, copyTestStepTest2Testsuite(step))
   144  		}
   145  
   146  		for _, step := range test.Spec.Steps {
   147  			testsuite.Spec.Steps = append(testsuite.Spec.Steps, copyTestStepTest2Testsuite(step))
   148  		}
   149  
   150  		for _, step := range test.Spec.After {
   151  			testsuite.Spec.After = append(testsuite.Spec.After, copyTestStepTest2Testsuite(step))
   152  		}
   153  
   154  		if _, err = m.testsuitesClient.Create(testsuite); err != nil {
   155  			return err
   156  		}
   157  
   158  		if err = m.testsClientV1.Delete(test.Name); err != nil {
   159  			return err
   160  		}
   161  	}
   162  
   163  	return nil
   164  }
   165  func (m *Version_0_9_2) Info() string {
   166  	return "Moving scripts v2 resources to tests v2 ones and tests v1 resources to testsuites v1 ones"
   167  }
   168  
   169  func (m *Version_0_9_2) Type() migrator.MigrationType {
   170  	return migrator.MigrationTypeServer
   171  }
   172  
   173  func copyTestStepTest2Testsuite(step testsv1.TestStepSpec) testsuite.TestSuiteStepSpec {
   174  	result := testsuite.TestSuiteStepSpec{
   175  		Type: testsuite.TestSuiteStepType(step.Type),
   176  	}
   177  
   178  	if step.Execute != nil {
   179  		result.Execute = &testsuite.TestSuiteStepExecute{
   180  			Namespace:     step.Execute.Namespace,
   181  			Name:          step.Execute.Name,
   182  			StopOnFailure: step.Execute.StopOnFailure,
   183  		}
   184  	}
   185  
   186  	if step.Delay != nil {
   187  		result.Delay = &testsuite.TestSuiteStepDelay{
   188  			Duration: step.Delay.Duration,
   189  		}
   190  	}
   191  
   192  	return result
   193  }