Repository: opentofu/opentofu
File: internal/backend/remote-state/s3/client_test.go (Go)

Code before:
				bucketName: "test-bucket",
				path:       "state-file",
				lockTags:   tt.lockTags,
			}
			err := tt.action(&rc)
			if err != nil {
				t.Fatalf("expected to have no error but got one: %s", err)
			}
			if httpCl.receivedReq == nil {
				t.Fatal("request didn't reach the mock http client")
			}
			gotTags := httpCl.receivedReq.Header.Get("x-amz-tagging")

			expectedTagsHeader := url.Values{}
			for k, v := range tt.lockTags {
				expectedTagsHeader.Add(k, v)
			}
			expectedTags := expectedTagsHeader.Encode()
			if gotTags != expectedTags {
				t.Errorf("Found tags %s did not match expected tags: %s", gotTags, expectedTags)
			}
		})
	}
}

func TestS3StateFileObjectTagging(t *testing.T) {
	// Configured the aws config the same way it is done for the backend to ensure a similar setup as the actual main logic.
	_, awsCfg, _ := awsbase.GetAwsConfig(context.Background(), &awsbase.Config{Region: "us-east-1", AccessKey: "test", SecretKey: "key"})
	httpCl := &mockHttpClient{resp: &http.Response{StatusCode: http.StatusOK, Body: io.NopCloser(strings.NewReader(""))}}
	s3Cl := s3.NewFromConfig(awsCfg, func(options *s3.Options) {
		options.HTTPClient = httpCl
	})

	tests := []struct {
		name         string
		action       func(cl *RemoteClient) error
		skipChecksum bool

		stateTags map[string]string
	}{
		{
			name:         "s3.Put with no tags",
			skipChecksum: false,
			action: func(cl *RemoteClient) error {
				return cl.Put(t.Context(), []byte("test"))
			},
			stateTags: map[string]string{},
		},
		{
			name:         "s3.Put with 1 tag",
			skipChecksum: false,

Review comment (suggestion):
```suggestion
func TestS3StateObjectTagging(t *testing.T) {
```

Code after:
			_, err := rc.Lock(t.Context(), statemgr.NewLockInfo())
			if err != nil {
				t.Fatalf("expected to have no error but got one: %s", err)
			}
			if httpCl.receivedReq == nil {
				t.Fatal("request didn't reach the mock http client")
			}
			gotTags := httpCl.receivedReq.Header.Get("x-amz-tagging")
			if gotTags != tt.expectedTags {
				t.Errorf("Found tags %s did not match expected tags: %s", gotTags, tt.expectedTags)
			}
		})
	}
}

func TestS3StateObjectTagging(t *testing.T) {
	// Configured the aws config the same way it is done for the backend to ensure a similar setup as the actual main logic.
	_, awsCfg, _ := awsbase.GetAwsConfig(context.Background(), &awsbase.Config{Region: "us-east-1", AccessKey: "test", SecretKey: "key"})
	httpCl := &mockHttpClient{resp: &http.Response{StatusCode: http.StatusOK, Body: io.NopCloser(strings.NewReader(""))}}
	s3Cl := s3.NewFromConfig(awsCfg, func(options *s3.Options) {
		options.HTTPClient = httpCl
	})

	tests := []struct {
		name         string
		action       func(cl *RemoteClient) error
		skipChecksum bool

		stateTags    map[string]string
		expectedTags string
	}{
		{
			name:         "s3.Put with no tags",
			skipChecksum: false,
			stateTags:    map[string]string{},
			expectedTags: "",
		},
		{
			name:         "s3.Put with 1 tag",
			skipChecksum: false,
			stateTags: map[string]string{
				"a": "b",
			},
			expectedTags: "a=b",
		},
		{
			name:         "s3.Put with several tags",
			skipChecksum: false,
			stateTags: map[string]string{
				"a": "b",
				"c": "e",