github.com/olli-ai/jx/v2@v2.0.400-0.20210921045218-14731b4dd448/pkg/tekton/tekton_helpers_test/test_helpers.go (about)

     1  package tekton_helpers_test
     2  
     3  import (
     4  	"io/ioutil"
     5  	"path/filepath"
     6  	"testing"
     7  
     8  	"github.com/ghodss/yaml"
     9  	v1 "github.com/jenkins-x/jx-api/pkg/apis/jenkins.io/v1"
    10  	"github.com/olli-ai/jx/v2/pkg/tests"
    11  	"github.com/olli-ai/jx/v2/pkg/util"
    12  	"github.com/stretchr/testify/assert"
    13  	"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1"
    14  	corev1 "k8s.io/api/core/v1"
    15  )
    16  
    17  // AssertLoadPods reads a file containing a PodList and returns that PodList
    18  func AssertLoadPods(t *testing.T, dir string) *corev1.PodList {
    19  	fileName := filepath.Join(dir, "pods.yml")
    20  	exists, err := util.FileExists(fileName)
    21  	if err != nil {
    22  		t.Fatalf("Error checking if file %s exists: %s", fileName, err)
    23  	}
    24  	if exists {
    25  		podList := &corev1.PodList{}
    26  		data, err := ioutil.ReadFile(fileName)
    27  		if assert.NoError(t, err, "Failed to load file %s", fileName) {
    28  			err = yaml.Unmarshal(data, podList)
    29  			if assert.NoError(t, err, "Failed to unmarshall YAML file %s", fileName) {
    30  				return podList
    31  			}
    32  
    33  		}
    34  	}
    35  	return &corev1.PodList{}
    36  }
    37  
    38  // AssertLoadSecret reads a file containing a PodList and returns that PodList
    39  func AssertLoadSecret(t *testing.T, dir string) *corev1.Secret {
    40  	fileName := filepath.Join(dir, "secret.yml")
    41  	exists, err := util.FileExists(fileName)
    42  	if err != nil {
    43  		t.Fatalf("Error checking if file %s exists: %s", fileName, err)
    44  	}
    45  	if exists {
    46  		secret := &corev1.Secret{}
    47  		data, err := ioutil.ReadFile(fileName)
    48  		if assert.NoError(t, err, "Failed to load file %s", fileName) {
    49  			err = yaml.Unmarshal(data, secret)
    50  			if assert.NoError(t, err, "Failed to unmarshall YAML file %s", fileName) {
    51  				return secret
    52  			}
    53  
    54  		}
    55  	}
    56  	return &corev1.Secret{}
    57  }
    58  
    59  // AssertLoadSinglePod reads a file containing a Pod and returns that Pod
    60  func AssertLoadSinglePod(t *testing.T, dir string) *corev1.Pod {
    61  	fileName := filepath.Join(dir, "pod.yml")
    62  	exists, err := util.FileExists(fileName)
    63  	if err != nil {
    64  		t.Fatalf("Error checking if file %s exists: %s", fileName, err)
    65  	}
    66  	if exists {
    67  		pod := &corev1.Pod{}
    68  		data, err := ioutil.ReadFile(fileName)
    69  		if assert.NoError(t, err, "Failed to load file %s", fileName) {
    70  			err = yaml.Unmarshal(data, pod)
    71  			if assert.NoError(t, err, "Failed to unmarshall YAML file %s", fileName) {
    72  				return pod
    73  			}
    74  
    75  		}
    76  	}
    77  	return &corev1.Pod{}
    78  }
    79  
    80  // AssertLoadPipelines reads a file containing a PipelineList and returns that PipelineList
    81  func AssertLoadPipelines(t *testing.T, dir string) *v1alpha1.PipelineList {
    82  	fileName := filepath.Join(dir, "pipelines.yml")
    83  	if tests.AssertFileExists(t, fileName) {
    84  		pipelineList := &v1alpha1.PipelineList{}
    85  		data, err := ioutil.ReadFile(fileName)
    86  		if assert.NoError(t, err, "Failed to load file %s", fileName) {
    87  			err = yaml.Unmarshal(data, pipelineList)
    88  			if assert.NoError(t, err, "Failed to unmarshal YAML file %s", fileName) {
    89  				return pipelineList
    90  			}
    91  
    92  		}
    93  	}
    94  	return nil
    95  }
    96  
    97  // AssertLoadSinglePipeline reads a file containing a Pipeline and returns that Pipeline
    98  func AssertLoadSinglePipeline(t *testing.T, dir string) *v1alpha1.Pipeline {
    99  	fileName := filepath.Join(dir, "pipeline.yml")
   100  	if tests.AssertFileExists(t, fileName) {
   101  		pipeline := &v1alpha1.Pipeline{}
   102  		data, err := ioutil.ReadFile(fileName)
   103  		if assert.NoError(t, err, "Failed to load file %s", fileName) {
   104  			err = yaml.Unmarshal(data, pipeline)
   105  			if assert.NoError(t, err, "Failed to unmarshall YAML file %s", fileName) {
   106  				return pipeline
   107  			}
   108  
   109  		}
   110  	}
   111  	return nil
   112  }
   113  
   114  // AssertLoadPipelineRuns reads a file containing a PipelineRunList and returns that PipelineRunList
   115  func AssertLoadPipelineRuns(t *testing.T, dir string) *v1alpha1.PipelineRunList {
   116  	fileName := filepath.Join(dir, "pipelineruns.yml")
   117  	if tests.AssertFileExists(t, fileName) {
   118  		pipelineRunList := &v1alpha1.PipelineRunList{}
   119  		data, err := ioutil.ReadFile(fileName)
   120  		if assert.NoError(t, err, "Failed to load file %s", fileName) {
   121  			err = yaml.Unmarshal(data, pipelineRunList)
   122  			if assert.NoError(t, err, "Failed to unmarshal YAML file %s", fileName) {
   123  				return pipelineRunList
   124  			}
   125  
   126  		}
   127  	}
   128  	return nil
   129  }
   130  
   131  // AssertLoadSinglePipelineRun reads a file containing a PipelineRun and returns that PipelineRun
   132  func AssertLoadSinglePipelineRun(t *testing.T, dir string) *v1alpha1.PipelineRun {
   133  	fileName := filepath.Join(dir, "pipelinerun.yml")
   134  	if tests.AssertFileExists(t, fileName) {
   135  		pipelineRun := &v1alpha1.PipelineRun{}
   136  		data, err := ioutil.ReadFile(fileName)
   137  		if assert.NoError(t, err, "Failed to load file %s", fileName) {
   138  			err = yaml.Unmarshal(data, pipelineRun)
   139  			if assert.NoError(t, err, "Failed to unmarshall YAML file %s", fileName) {
   140  				return pipelineRun
   141  			}
   142  
   143  		}
   144  	}
   145  	return nil
   146  }
   147  
   148  // AssertLoadPipelineActivities reads a file containing a PipelineActivityList and returns that PipelineActivityList
   149  func AssertLoadPipelineActivities(t *testing.T, dir string) *v1.PipelineActivityList {
   150  	fileName := filepath.Join(dir, "activities.yml")
   151  	if tests.AssertFileExists(t, fileName) {
   152  		activityList := &v1.PipelineActivityList{}
   153  		data, err := ioutil.ReadFile(fileName)
   154  		if assert.NoError(t, err, "Failed to load file %s", fileName) {
   155  			err = yaml.Unmarshal(data, activityList)
   156  			if assert.NoError(t, err, "Failed to unmarshal YAML file %s", fileName) {
   157  				return activityList
   158  			}
   159  
   160  		}
   161  	}
   162  	return nil
   163  }
   164  
   165  // AssertLoadSinglePipelineActivity reads a file containing a PipelineActivity and returns that PipelineActivity
   166  func AssertLoadSinglePipelineActivity(t *testing.T, dir string) *v1.PipelineActivity {
   167  	fileName := filepath.Join(dir, "activity.yml")
   168  	if tests.AssertFileExists(t, fileName) {
   169  		activity := &v1.PipelineActivity{}
   170  		data, err := ioutil.ReadFile(fileName)
   171  		if assert.NoError(t, err, "Failed to load file %s", fileName) {
   172  			err = yaml.Unmarshal(data, activity)
   173  			if assert.NoError(t, err, "Failed to unmarshall YAML file %s", fileName) {
   174  				return activity
   175  			}
   176  
   177  		}
   178  	}
   179  	return nil
   180  }
   181  
   182  // AssertLoadPipelineStructures reads a file containing a PipelineStructureList and returns that PipelineStructureList
   183  func AssertLoadPipelineStructures(t *testing.T, dir string) *v1.PipelineStructureList {
   184  	fileName := filepath.Join(dir, "structures.yml")
   185  	if tests.AssertFileExists(t, fileName) {
   186  		structureList := &v1.PipelineStructureList{}
   187  		data, err := ioutil.ReadFile(fileName)
   188  		if assert.NoError(t, err, "Failed to load file %s", fileName) {
   189  			err = yaml.Unmarshal(data, structureList)
   190  			if assert.NoError(t, err, "Failed to unmarshal YAML file %s", fileName) {
   191  				return structureList
   192  			}
   193  
   194  		}
   195  	}
   196  	return nil
   197  }
   198  
   199  // AssertLoadSinglePipelineStructure reads a file containing a PipelineStructure and returns that PipelineStructure
   200  func AssertLoadSinglePipelineStructure(t *testing.T, dir string) *v1.PipelineStructure {
   201  	fileName := filepath.Join(dir, "structure.yml")
   202  	exists, err := util.FileExists(fileName)
   203  	if err != nil {
   204  		t.Fatalf("Error checking if file %s exists: %s", fileName, err)
   205  	}
   206  	if exists {
   207  		structure := &v1.PipelineStructure{}
   208  		data, err := ioutil.ReadFile(fileName)
   209  		if assert.NoError(t, err, "Failed to load file %s", fileName) {
   210  			err = yaml.Unmarshal(data, structure)
   211  			if assert.NoError(t, err, "Failed to unmarshall YAML file %s", fileName) {
   212  				return structure
   213  			}
   214  
   215  		}
   216  	}
   217  	return nil
   218  }
   219  
   220  // AssertLoadTasks reads a file containing a TaskList and returns that TaskList
   221  func AssertLoadTasks(t *testing.T, dir string) *v1alpha1.TaskList {
   222  	fileName := filepath.Join(dir, "tasks.yml")
   223  	if tests.AssertFileExists(t, fileName) {
   224  		taskList := &v1alpha1.TaskList{}
   225  		data, err := ioutil.ReadFile(fileName)
   226  		if assert.NoError(t, err, "Failed to load file %s", fileName) {
   227  			err = yaml.Unmarshal(data, taskList)
   228  			if assert.NoError(t, err, "Failed to unmarshall YAML file %s", fileName) {
   229  				return taskList
   230  			}
   231  
   232  		}
   233  	}
   234  	return nil
   235  }
   236  
   237  // AssertLoadTaskRuns reads a file containing a TaskRunList and returns that TaskRunList
   238  func AssertLoadTaskRuns(t *testing.T, dir string) *v1alpha1.TaskRunList {
   239  	fileName := filepath.Join(dir, "taskruns.yml")
   240  	if tests.AssertFileExists(t, fileName) {
   241  		taskRunList := &v1alpha1.TaskRunList{}
   242  		data, err := ioutil.ReadFile(fileName)
   243  		if assert.NoError(t, err, "Failed to load file %s", fileName) {
   244  			err = yaml.Unmarshal(data, taskRunList)
   245  			if assert.NoError(t, err, "Failed to unmarshall YAML file %s", fileName) {
   246  				return taskRunList
   247  			}
   248  
   249  		}
   250  	}
   251  	return nil
   252  }
   253  
   254  // AssertLoadPipelineResources reads a file containing a PipelineResourceList and returns that PipelineResourceList
   255  func AssertLoadPipelineResources(t *testing.T, dir string) *v1alpha1.PipelineResourceList {
   256  	fileName := filepath.Join(dir, "pipelineresources.yml")
   257  	if tests.AssertFileExists(t, fileName) {
   258  		resourceList := &v1alpha1.PipelineResourceList{}
   259  		data, err := ioutil.ReadFile(fileName)
   260  		if assert.NoError(t, err, "Failed to load file %s", fileName) {
   261  			err = yaml.Unmarshal(data, resourceList)
   262  			if assert.NoError(t, err, "Failed to unmarshall YAML file %s", fileName) {
   263  				return resourceList
   264  			}
   265  
   266  		}
   267  	}
   268  	return nil
   269  }