k8s.io/kubernetes@v1.31.0-alpha.0.0.20240520171757-56147500dadc/cluster/gce/gci/append_or_replace_prefixed_line_test.go (about)

     1  /*
     2  Copyright 2020 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package gci
    18  
    19  import (
    20  	"fmt"
    21  	"os"
    22  	"os/exec"
    23  	"testing"
    24  
    25  	"github.com/google/go-cmp/cmp"
    26  )
    27  
    28  func TestAppendOrReplacePrefix(t *testing.T) {
    29  	testCases := []struct {
    30  		desc                string
    31  		prefix              string
    32  		suffix              string
    33  		initialFileContents string
    34  		want                string
    35  	}{
    36  		{
    37  			desc:   "simple string and empty file",
    38  			prefix: "hello",
    39  			suffix: "world",
    40  			want: `helloworld
    41  `,
    42  		},
    43  		{
    44  			desc:   "simple string and non empty file",
    45  			prefix: "hello",
    46  			suffix: "world",
    47  			initialFileContents: `jelloworld
    48  chelloworld
    49  `,
    50  			want: `jelloworld
    51  chelloworld
    52  helloworld
    53  `,
    54  		},
    55  		{
    56  			desc:   "simple string and file already contains prefix",
    57  			prefix: "hello",
    58  			suffix: "world",
    59  			initialFileContents: `helloworld
    60  helloworld
    61  jelloworld
    62  chelloworld
    63  `,
    64  			want: `jelloworld
    65  chelloworld
    66  helloworld
    67  `,
    68  		},
    69  		{
    70  			desc:   "simple string and file already contains prefix with content between the prefix and suffix",
    71  			prefix: "hello",
    72  			suffix: "world",
    73  			initialFileContents: `hellocontentsworld
    74  jelloworld
    75  chelloworld
    76  `,
    77  			want: `jelloworld
    78  chelloworld
    79  helloworld
    80  `,
    81  		},
    82  		{
    83  			desc:   "simple string and file already contains prefix with prefix == suffix",
    84  			prefix: "hello",
    85  			suffix: "world",
    86  			initialFileContents: `hellohello
    87  jelloworld
    88  chelloworld
    89  `,
    90  			want: `jelloworld
    91  chelloworld
    92  helloworld
    93  `,
    94  		},
    95  		{
    96  			desc:   "string with quotes and = and empty file",
    97  			prefix: `'"$argon2id$v=19"'`,
    98  			suffix: "admin",
    99  			want: `"$argon2id$v=19"admin
   100  `,
   101  		},
   102  		{
   103  			desc:   "string with quotes and = and non empty file",
   104  			prefix: `'"$argon2id$v=19"'`,
   105  			suffix: "admin",
   106  			initialFileContents: `jelloworld
   107  chelloworld
   108  `,
   109  			want: `jelloworld
   110  chelloworld
   111  "$argon2id$v=19"admin
   112  `,
   113  		},
   114  		{
   115  			desc:   "string with quotes and = and file already contains prefix",
   116  			prefix: `'"$argon2id$v=19"'`,
   117  			suffix: "admin",
   118  			initialFileContents: `"$argon2id$v=19"admin
   119  "$argon2id$v=19"admin
   120  helloworld
   121  jelloworld
   122  `,
   123  			want: `helloworld
   124  jelloworld
   125  "$argon2id$v=19"admin
   126  `,
   127  		},
   128  		{
   129  			desc:   "string with quotes and = and file already contains prefix with content between the prefix and suffix",
   130  			prefix: `'"$argon2id$v=19"'`,
   131  			suffix: "admin",
   132  			initialFileContents: `"$argon2id$v=19"contentsadmin
   133  helloworld
   134  jelloworld
   135  `,
   136  			want: `helloworld
   137  jelloworld
   138  "$argon2id$v=19"admin
   139  `,
   140  		},
   141  		{
   142  			desc:   "string with quotes and = and file already contains prefix with prefix == suffix",
   143  			prefix: `'"$argon2id$v=19"'`,
   144  			suffix: "admin",
   145  			initialFileContents: `"$argon2id$v=19""$argon2id$v=19"
   146  helloworld
   147  jelloworld
   148  `,
   149  			want: `helloworld
   150  jelloworld
   151  "$argon2id$v=19"admin
   152  `,
   153  		},
   154  	}
   155  	for _, tc := range testCases {
   156  		t.Run(tc.desc, func(t *testing.T) {
   157  			f, err := os.CreateTemp("", "append_or_replace_test")
   158  			if err != nil {
   159  				t.Fatalf("Failed to create temp file: %v", err)
   160  			}
   161  			defer os.Remove(f.Name())
   162  			if _, err := f.WriteString(tc.initialFileContents); err != nil {
   163  				t.Fatalf("Failed to write to file: %v", err)
   164  			}
   165  			f.Close()
   166  			args := fmt.Sprintf("source configure-helper.sh; append_or_replace_prefixed_line %s %s %s", f.Name(), tc.prefix, tc.suffix)
   167  			cmd := exec.Command("bash", "-c", args)
   168  			stderr, err := cmd.CombinedOutput()
   169  			if err != nil {
   170  				t.Fatalf("Failed to run command: %v: %s", err, stderr)
   171  			}
   172  			got, err := os.ReadFile(f.Name())
   173  			if err != nil {
   174  				t.Fatalf("Failed to read file contents: %v", err)
   175  			}
   176  			if diff := cmp.Diff(string(got), tc.want); diff != "" {
   177  				t.Errorf("File contents: got=%s, want=%s, diff=%s", got, tc.want, diff)
   178  			}
   179  		})
   180  	}
   181  
   182  }