From eb9b671073c84954f37775f6aedb77f24e5614f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A9sz=C3=A1ros=20Gergely?= Date: Wed, 24 Jul 2024 13:07:55 +0200 Subject: [PATCH] CDPCP-12516 - clean up unused functions prior implementing dead code check --- .github/workflows/test.yml | 2 +- cdp-sdk-go/cdp/errors.go | 13 +-- .../{test_common.go => test_common_utils.go} | 0 utils/file_reader.go | 30 ------- utils/file_reader_test.go | 89 ------------------- .../{string_utils.go => test_string_utils.go} | 0 ...tils_test.go => test_string_utils_test.go} | 0 7 files changed, 3 insertions(+), 131 deletions(-) rename resources/iam/{test_common.go => test_common_utils.go} (100%) delete mode 100644 utils/file_reader.go delete mode 100644 utils/file_reader_test.go rename utils/{string_utils.go => test_string_utils.go} (100%) rename utils/{string_utils_test.go => test_string_utils_test.go} (100%) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index ff10cb7b..87d23487 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.9 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/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