github.com/xgoffin/jenkins-library@v1.154.0/cmd/containerExecuteStructureTests_test.go (about)

     1  package cmd
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"testing"
     7  
     8  	"github.com/SAP/jenkins-library/pkg/mock"
     9  	"github.com/pkg/errors"
    10  	"github.com/stretchr/testify/assert"
    11  )
    12  
    13  type containerStructureTestsMockUtils struct {
    14  	shouldFail     bool
    15  	requestedUrls  []string
    16  	requestedFiles []string
    17  	*mock.FilesMock
    18  	*mock.ExecMockRunner
    19  }
    20  
    21  func (m *containerStructureTestsMockUtils) Glob(pattern string) (matches []string, err error) {
    22  	switch pattern {
    23  	case "**.yaml":
    24  		return []string{"config1.yaml", "config2.yaml"}, nil
    25  	case "empty":
    26  		return []string{}, nil
    27  	case "error":
    28  		return nil, errors.New("failed to find fies")
    29  	}
    30  
    31  	return nil, nil
    32  }
    33  
    34  func newContainerStructureTestsMockUtils() containerStructureTestsMockUtils {
    35  	utils := containerStructureTestsMockUtils{
    36  		shouldFail:     false,
    37  		FilesMock:      &mock.FilesMock{},
    38  		ExecMockRunner: &mock.ExecMockRunner{},
    39  	}
    40  	return utils
    41  }
    42  
    43  func TestRunContainerExecuteStructureTests(t *testing.T) {
    44  
    45  	t.Run("success case", func(t *testing.T) {
    46  		config := &containerExecuteStructureTestsOptions{
    47  			PullImage:          true,
    48  			TestConfiguration:  "**.yaml",
    49  			TestDriver:         "docker",
    50  			TestImage:          "reg/image:tag",
    51  			TestReportFilePath: "report.json",
    52  		}
    53  
    54  		mockUtils := newContainerStructureTestsMockUtils()
    55  
    56  		// test
    57  		err := runContainerExecuteStructureTests(config, &mockUtils)
    58  		// assert
    59  		expectedParams := []string{
    60  			"test",
    61  			"--config", "config1.yaml",
    62  			"--config", "config2.yaml",
    63  			"--driver", "docker",
    64  			"--pull",
    65  			"--image", "reg/image:tag",
    66  			"--test-report", "report.json",
    67  		}
    68  
    69  		assert.NoError(t, err)
    70  		if assert.Equal(t, 1, len(mockUtils.Calls)) {
    71  			assert.Equal(t, "container-structure-test", mockUtils.Calls[0].Exec)
    72  			assert.Equal(t, expectedParams, mockUtils.Calls[0].Params)
    73  		}
    74  	})
    75  
    76  	t.Run("success case - without pulling image", func(t *testing.T) {
    77  		config := &containerExecuteStructureTestsOptions{
    78  			TestConfiguration:  "**.yaml",
    79  			TestDriver:         "docker",
    80  			TestImage:          "reg/image:tag",
    81  			TestReportFilePath: "report.json",
    82  		}
    83  
    84  		mockUtils := newContainerStructureTestsMockUtils()
    85  
    86  		// test
    87  		err := runContainerExecuteStructureTests(config, &mockUtils)
    88  		// assert
    89  		expectedParams := []string{
    90  			"test",
    91  			"--config", "config1.yaml",
    92  			"--config", "config2.yaml",
    93  			"--driver", "docker",
    94  			"--image", "reg/image:tag",
    95  			"--test-report", "report.json",
    96  		}
    97  
    98  		assert.NoError(t, err)
    99  		if assert.Equal(t, 1, len(mockUtils.Calls)) {
   100  			assert.Equal(t, "container-structure-test", mockUtils.Calls[0].Exec)
   101  			assert.Equal(t, expectedParams, mockUtils.Calls[0].Params)
   102  		}
   103  	})
   104  
   105  	t.Run("success case - verbose", func(t *testing.T) {
   106  		GeneralConfig.Verbose = true
   107  		config := &containerExecuteStructureTestsOptions{
   108  			TestConfiguration:  "**.yaml",
   109  			TestDriver:         "docker",
   110  			TestImage:          "reg/image:tag",
   111  			TestReportFilePath: "report.json",
   112  		}
   113  
   114  		mockUtils := newContainerStructureTestsMockUtils()
   115  
   116  		// test
   117  		err := runContainerExecuteStructureTests(config, &mockUtils)
   118  		// assert
   119  		expectedParams := []string{
   120  			"test",
   121  			"--config", "config1.yaml",
   122  			"--config", "config2.yaml",
   123  			"--driver", "docker",
   124  			"--image", "reg/image:tag",
   125  			"--test-report", "report.json",
   126  			"--verbosity", "debug",
   127  		}
   128  
   129  		assert.NoError(t, err)
   130  		if assert.Equal(t, 1, len(mockUtils.Calls)) {
   131  			assert.Equal(t, "container-structure-test", mockUtils.Calls[0].Exec)
   132  			assert.Equal(t, expectedParams, mockUtils.Calls[0].Params)
   133  		}
   134  		GeneralConfig.Verbose = false
   135  	})
   136  
   137  	t.Run("success case - run on k8s", func(t *testing.T) {
   138  		if err := os.Setenv("ON_K8S", "true"); err != nil {
   139  			t.Error(err)
   140  		}
   141  		config := &containerExecuteStructureTestsOptions{
   142  			TestConfiguration:  "**.yaml",
   143  			TestImage:          "reg/image:tag",
   144  			TestReportFilePath: "report.json",
   145  		}
   146  
   147  		mockUtils := newContainerStructureTestsMockUtils()
   148  
   149  		// test
   150  		err := runContainerExecuteStructureTests(config, &mockUtils)
   151  		// assert
   152  		expectedParams := []string{
   153  			"test",
   154  			"--config", "config1.yaml",
   155  			"--config", "config2.yaml",
   156  			"--driver", "tar",
   157  			"--image", "reg/image:tag",
   158  			"--test-report", "report.json",
   159  		}
   160  
   161  		assert.NoError(t, err)
   162  		if assert.Equal(t, 1, len(mockUtils.Calls)) {
   163  			assert.Equal(t, "container-structure-test", mockUtils.Calls[0].Exec)
   164  			assert.Equal(t, expectedParams, mockUtils.Calls[0].Params)
   165  		}
   166  		os.Unsetenv("ON_K8S")
   167  	})
   168  
   169  	t.Run("error case - execution failed", func(t *testing.T) {
   170  		config := &containerExecuteStructureTestsOptions{
   171  			PullImage:          true,
   172  			TestConfiguration:  "**.yaml",
   173  			TestDriver:         "docker",
   174  			TestImage:          "reg/image:tag",
   175  			TestReportFilePath: "report.json",
   176  		}
   177  		mockUtils := newContainerStructureTestsMockUtils()
   178  		mockUtils.ExecMockRunner = &mock.ExecMockRunner{
   179  			ShouldFailOnCommand: map[string]error{"container-structure-test": fmt.Errorf("container-structure-test run failed")},
   180  		}
   181  
   182  		// test
   183  		err := runContainerExecuteStructureTests(config, &mockUtils)
   184  		// assert
   185  		assert.EqualError(t, err, "failed to run executable, command: '[container-structure-test test --config config1.yaml --config config2.yaml --driver docker --pull --image reg/image:tag --test-report report.json]', error: container-structure-test run failed: container-structure-test run failed")
   186  	})
   187  
   188  	t.Run("error case - configuration is missing", func(t *testing.T) {
   189  		config := &containerExecuteStructureTestsOptions{
   190  			PullImage:          true,
   191  			TestConfiguration:  "empty",
   192  			TestDriver:         "docker",
   193  			TestReportFilePath: "report.json",
   194  		}
   195  		mockUtils := newContainerStructureTestsMockUtils()
   196  
   197  		// test
   198  		err := runContainerExecuteStructureTests(config, &mockUtils)
   199  		// assert
   200  		assert.EqualError(t, err, "config files mustn't be missing")
   201  	})
   202  
   203  	t.Run("error case - failed to find config files", func(t *testing.T) {
   204  		config := &containerExecuteStructureTestsOptions{
   205  			PullImage:          true,
   206  			TestConfiguration:  "error",
   207  			TestDriver:         "docker",
   208  			TestReportFilePath: "report.json",
   209  		}
   210  		mockUtils := newContainerStructureTestsMockUtils()
   211  
   212  		// test
   213  		err := runContainerExecuteStructureTests(config, &mockUtils)
   214  		// assert
   215  		assert.EqualError(t, err, "failed to find config files, error: failed to find fies: failed to find fies")
   216  	})
   217  
   218  	t.Run("error case - incorrect driver type", func(t *testing.T) {
   219  		config := &containerExecuteStructureTestsOptions{
   220  			PullImage:          true,
   221  			TestConfiguration:  "**.yaml",
   222  			TestDriver:         "wrongDriver",
   223  			TestReportFilePath: "report.json",
   224  		}
   225  		mockUtils := newContainerStructureTestsMockUtils()
   226  
   227  		// test
   228  		err := runContainerExecuteStructureTests(config, &mockUtils)
   229  		// assert
   230  		assert.EqualError(t, err, "test driver wrongDriver is incorrect. Possible drivers: docker, tar")
   231  	})
   232  }