diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index ff10cb7b..b0a8b483 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -100,7 +100,7 @@ jobs: - name: Go Coverage uses: gwatts/go-coverage-action@v2.0.0 with: - coverage-threshold: 32.0 + coverage-threshold: 31.8 cover-pkg: ./... ignore-pattern: | /cdp-sdk-go/ diff --git a/cdp-sdk-go/cdp/errors.go b/cdp-sdk-go/cdp/errors.go index 5b97db46..46fb3fcb 100644 --- a/cdp-sdk-go/cdp/errors.go +++ b/cdp-sdk-go/cdp/errors.go @@ -11,21 +11,16 @@ package cdp import ( + "strings" + datahubmodels "github.com/cloudera/terraform-provider-cdp/cdp-sdk-go/gen/datahub/models" datalakemodels "github.com/cloudera/terraform-provider-cdp/cdp-sdk-go/gen/datalake/models" environmentsmodels "github.com/cloudera/terraform-provider-cdp/cdp-sdk-go/gen/environments/models" - iammodels "github.com/cloudera/terraform-provider-cdp/cdp-sdk-go/gen/iam/models" - mlmodels "github.com/cloudera/terraform-provider-cdp/cdp-sdk-go/gen/ml/models" opdbmodels "github.com/cloudera/terraform-provider-cdp/cdp-sdk-go/gen/opdb/models" - "strings" ) // These functions should be generated in the appropriate gen package in an errors module. -func IsIamError(err *iammodels.Error, code string, message string) bool { - return err.Code == code && strings.Contains(err.Message, message) -} - func IsEnvironmentsError(err *environmentsmodels.Error, code string, message string) bool { return err.Code == code && strings.Contains(err.Message, message) } @@ -41,7 +36,3 @@ func IsDatahubError(err *datahubmodels.Error, code string, message string) bool func IsDatabaseError(err *opdbmodels.Error, code string, message string) bool { return err.Code == code && strings.Contains(err.Message, message) } - -func IsMlError(err *mlmodels.Error, code string, message string) bool { - return err.Code == code && strings.Contains(err.Message, message) -} diff --git a/resources/iam/test_common.go b/resources/iam/test_common_utils.go similarity index 100% rename from resources/iam/test_common.go rename to resources/iam/test_common_utils.go diff --git a/utils/file_reader.go b/utils/file_reader.go deleted file mode 100644 index 9eb94d93..00000000 --- a/utils/file_reader.go +++ /dev/null @@ -1,30 +0,0 @@ -// Copyright 2023 Cloudera. All Rights Reserved. -// -// This file is licensed under the Apache License Version 2.0 (the "License"). -// You may not use this file except in compliance with the License. -// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0. -// -// This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS -// OF ANY KIND, either express or implied. Refer to the License for the specific -// permissions and limitations governing your use of the file. - -package utils - -import ( - "context" - "fmt" - "github.com/hashicorp/terraform-plugin-log/tflog" - "os" -) - -func ReadFileContent(ctx context.Context, path string) (*string, error) { - tflog.Info(ctx, fmt.Sprintf("About to read file on path: %s", path)) - data, err := os.ReadFile(path) - if err != nil { - tflog.Warn(ctx, fmt.Sprintf("Error occurred during file read: %s", err.Error())) - return nil, err - } - tflog.Info(ctx, "Reading file was successful.") - content := string(data) - return &content, nil -} diff --git a/utils/file_reader_test.go b/utils/file_reader_test.go deleted file mode 100644 index 91670e2a..00000000 --- a/utils/file_reader_test.go +++ /dev/null @@ -1,89 +0,0 @@ -// Copyright 2023 Cloudera. All Rights Reserved. -// -// This file is licensed under the Apache License Version 2.0 (the "License"). -// You may not use this file except in compliance with the License. -// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0. -// -// This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS -// OF ANY KIND, either express or implied. Refer to the License for the specific -// permissions and limitations governing your use of the file. - -package utils - -import ( - "context" - "log" - "os" - "runtime" - "testing" - - "github.com/hectane/go-acl" -) - -const FILE_NO_PERMISSIOM = os.FileMode(000) - -func TestWhenFileDoesNotExists(t *testing.T) { - content, err := ReadFileContent(context.TODO(), "someNonExistingStuff") - checkFailure(t, content, err) -} - -func TestReadFileContentWhenNoPermissionToRead(t *testing.T) { - file, err := os.CreateTemp(os.TempDir(), "gcp_read_temp") - if err != nil { - t.Errorf("Unable to prepare temprary file for testing due to: " + err.Error()) - } - defer func(file *os.File) { - err := file.Close() - if err != nil { - log.Default().Printf("unable to clean up file ('%s') due to: %s", file.Name(), err.Error()) - } - }(file) - defer func(name string) { - err := os.Remove(name) - if err != nil { - log.Default().Printf("unable to clean up file ('%s') due to: %s", file.Name(), err.Error()) - } - }(file.Name()) - if runtime.GOOS == `windows` { - err = acl.Chmod(file.Name(), FILE_NO_PERMISSIOM) - } else { - err = os.Chmod(file.Name(), FILE_NO_PERMISSIOM) - } - if err != nil { - t.Errorf("Unable to update temprary file's permission for testing due to: " + err.Error()) - } - - content, err := ReadFileContent(context.TODO(), file.Name()) - - checkFailure(t, content, err) -} - -func TestReadFileContentWhenFileExistsAndHavePermission(t *testing.T) { - file, err := os.CreateTemp(os.TempDir(), "gcp_read_temp") - if err != nil { - t.Errorf("Unable to prepare temprary file for testing due to: " + err.Error()) - } - originalContent := []byte(`{"some":"amazing","content":true}`) - if _, err = file.Write(originalContent); err != nil { - t.Errorf("Unable to update temp file ('%s') content due to: %s\n", file.Name(), err.Error()) - } - - resultContent, err := ReadFileContent(context.TODO(), file.Name()) - - if err != nil { - t.Errorf("File read failed due to: %s", err.Error()) - } - originalContentToCompare := string(originalContent) - if *resultContent != originalContentToCompare { - t.Errorf("After file read it did not return the expected content! Expected: %s, got: %s", originalContentToCompare, *resultContent) - } -} - -func checkFailure(t *testing.T, content *string, readError error) { - if readError == nil { - t.Error("Expected read failure did not happen!") - } - if content != nil { - t.Error("Content should not be filled when error happen!") - } -} diff --git a/utils/slice.go b/utils/slice.go index 26e4ae5a..cd71ad34 100644 --- a/utils/slice.go +++ b/utils/slice.go @@ -22,16 +22,3 @@ func ContainsAsSubstring(slice []string, element string) bool { } return false } - -func ContainsEitherSubstring(slice []string, elements []string) bool { - if len(slice) > 0 && len(elements) > 0 { - for _, e := range slice { - for _, substring := range elements { - if strings.Contains(e, substring) { - return true - } - } - } - } - return false -} diff --git a/utils/slice_test.go b/utils/slice_test.go index c34b6bf0..fc6ec04a 100644 --- a/utils/slice_test.go +++ b/utils/slice_test.go @@ -10,7 +10,10 @@ package utils -import "testing" +import ( + "strings" + "testing" +) func TestContainsAsSubstring(t *testing.T) { type input struct { @@ -117,3 +120,16 @@ func TestContainsEitherSubstring(t *testing.T) { }) } } + +func ContainsEitherSubstring(slice []string, elements []string) bool { + if len(slice) > 0 && len(elements) > 0 { + for _, e := range slice { + for _, substring := range elements { + if strings.Contains(e, substring) { + return true + } + } + } + } + return false +} diff --git a/utils/string_utils.go b/utils/test_string_utils.go similarity index 100% rename from utils/string_utils.go rename to utils/test_string_utils.go diff --git a/utils/string_utils_test.go b/utils/test_string_utils_test.go similarity index 100% rename from utils/string_utils_test.go rename to utils/test_string_utils_test.go