github.com/filecoin-project/bacalhau@v0.3.23-0.20230228154132-45c989550ace/cmd/bacalhau/utils_test.go (about) 1 //go:build unit || !integration 2 3 package bacalhau 4 5 import ( 6 "context" 7 "testing" 8 9 "github.com/filecoin-project/bacalhau/pkg/job" 10 "github.com/filecoin-project/bacalhau/pkg/logger" 11 "github.com/filecoin-project/bacalhau/pkg/model" 12 "github.com/filecoin-project/bacalhau/pkg/system" 13 "github.com/stretchr/testify/require" 14 "github.com/stretchr/testify/suite" 15 ) 16 17 func TestUtilsSuite(t *testing.T) { 18 suite.Run(t, new(UtilsSuite)) 19 } 20 21 // Define the suite, and absorb the built-in basic suite 22 // functionality from testify - including a T() method which 23 // returns the current testing context 24 type UtilsSuite struct { 25 suite.Suite 26 } 27 28 // Before each test 29 func (s *UtilsSuite) SetupTest() { 30 logger.ConfigureTestLogging(s.T()) 31 } 32 33 func (s *UtilsSuite) TestSafeRegex() { 34 // Put a few examples at the front, for manual testing 35 tests := []struct { 36 stringToTest string 37 predictedLength int // set to -1 if skip test 38 }{ 39 {stringToTest: "abc123-", predictedLength: 7}, // Nothing should be stripped 40 {stringToTest: `"'@123`, predictedLength: 4}, // Should leave just 123 41 {stringToTest: "👫ðŸ‘👲👴", predictedLength: len("👫ðŸ‘👲👴")}, // Emojis should work 42 } 43 44 for _, tc := range tests { 45 strippedString := job.SafeStringStripper(tc.stringToTest) 46 require.LessOrEqual(s.T(), len(strippedString), len(tc.stringToTest)) 47 if tc.predictedLength >= 0 { 48 require.Equal(s.T(), tc.predictedLength, len(strippedString)) 49 } 50 } 51 } 52 53 func (s *UtilsSuite) TestVersionCheck() { 54 require.NoError(s.T(), system.InitConfigForTesting(s.T())) 55 56 // OK: Normal operation 57 err := ensureValidVersion(context.TODO(), &model.BuildVersionInfo{ 58 GitVersion: "v1.2.3", 59 }, &model.BuildVersionInfo{ 60 GitVersion: "v1.2.3", 61 }) 62 require.NoError(s.T(), err) 63 64 // OK: invalid semver 65 err = ensureValidVersion(context.TODO(), &model.BuildVersionInfo{ 66 GitVersion: "not-a-sem-ver", 67 }, &model.BuildVersionInfo{ 68 GitVersion: "v1.2.0", 69 }) 70 require.NoError(s.T(), err) 71 72 // OK: nil semver 73 err = ensureValidVersion(context.TODO(), nil, &model.BuildVersionInfo{ 74 GitVersion: "v1.2.0", 75 }) 76 require.NoError(s.T(), err) 77 78 // OK: development version 79 err = ensureValidVersion(context.TODO(), &model.BuildVersionInfo{ 80 GitVersion: "v0.0.0-xxxxxxx", 81 }, &model.BuildVersionInfo{ 82 GitVersion: "v1.2.0", 83 }) 84 require.NoError(s.T(), err) 85 86 // OK: development version 87 err = ensureValidVersion(context.TODO(), &model.BuildVersionInfo{ 88 GitVersion: "v1.2.0", 89 }, &model.BuildVersionInfo{ 90 GitVersion: "v0.0.0-xxxxxxx", 91 }) 92 require.NoError(s.T(), err) 93 94 // NOT OK: server is newer 95 err = ensureValidVersion(context.TODO(), &model.BuildVersionInfo{ 96 GitVersion: "v1.2.3", 97 }, &model.BuildVersionInfo{ 98 GitVersion: "v1.2.4", 99 }) 100 require.Error(s.T(), err) 101 102 // NOT OK: client is newer 103 err = ensureValidVersion(context.TODO(), &model.BuildVersionInfo{ 104 GitVersion: "v1.2.4", 105 }, &model.BuildVersionInfo{ 106 GitVersion: "v1.2.3", 107 }) 108 require.Error(s.T(), err) 109 110 // https://github.com/filecoin-project/bacalhau/issues/495 111 err = ensureValidVersion(context.TODO(), &model.BuildVersionInfo{ 112 GitVersion: "v0.1.37", 113 }, &model.BuildVersionInfo{ 114 GitVersion: "v0.1.36", 115 }) 116 require.Error(s.T(), err) 117 require.Contains(s.T(), err.Error(), "client version v0.1.37") 118 } 119 120 func (s *UtilsSuite) TestImages() { 121 tc := map[string]struct { 122 Image string 123 Valid bool 124 }{ 125 // TODO: #843 Unblock when we can figure out how to check the existence of the image 126 // "no image": { 127 // image: "", 128 // valid: false, 129 // }, 130 // "invalid image": { 131 // image: "badimageNOTFOUND", 132 // valid: false, 133 // }, 134 "image with tag (norepo)": { 135 Image: "ubuntu:latest", 136 Valid: true, 137 }, 138 "image with tag (repo)": { 139 Image: "curlimages/curl:7.85.0", 140 Valid: true, 141 }, 142 } 143 144 for name, test := range tc { 145 s.Run(name, func() { 146 sampleJob, _ := model.NewJobWithSaneProductionDefaults() 147 sampleJob.Spec.Docker.Image = test.Image 148 err := job.VerifyJob(context.TODO(), sampleJob) 149 if test.Valid { 150 require.NoError(s.T(), err, "%s: expected valid image %s to pass", name, test.Image) 151 } else { 152 require.Error(s.T(), err, "%s: expected invalid image %s to fail", name, test.Image) 153 } 154 }) 155 } 156 }