@@ -61,33 +61,65 @@ func TestUpdateHardenRunnerConfig(t *testing.T) {
6161
6262 blockConfig := "- name: Harden the runner (Audit all outbound calls)\n uses: step-security/harden-runner@v2\n with:\n egress-policy: block\n allowed-endpoints: >\n github.com:443\n api.github.com:443"
6363
64+ blockConfigWithComments := "# Harden Runner step added by StepSecurity\n - name: Harden the runner (Audit all outbound calls)\n uses: step-security/harden-runner@v2\n with:\n egress-policy: block\n # Approved endpoints for CI\n allowed-endpoints: >\n github.com:443\n api.github.com:443\n # npm registry\n registry.npmjs.org:443"
65+
6466 tests := []struct {
6567 name string
68+ inputFile string
6669 config HardenRunnerConfig
6770 wantUpdated bool
6871 outputFile string
6972 }{
7073 {
7174 name : "subtractive true replaces existing config" ,
75+ inputFile : "updateConfig.yml" ,
7276 config : HardenRunnerConfig {Config : blockConfig , Subtractive : true },
7377 wantUpdated : true ,
7478 outputFile : "updateConfig.yml" ,
7579 },
7680 {
7781 name : "subtractive false does not change existing config" ,
82+ inputFile : "updateConfig.yml" ,
7883 config : HardenRunnerConfig {Config : blockConfig , Subtractive : false },
7984 wantUpdated : false ,
8085 outputFile : "updateConfigNotSubtractive.yml" ,
8186 },
82- }
83-
84- input , err := ioutil .ReadFile (path .Join (inputDirectory , "updateConfig.yml" ))
85- if err != nil {
86- t .Fatalf ("error reading input file: %v" , err )
87+ {
88+ name : "subtractive replaces existing allowed-endpoints" ,
89+ inputFile : "updateConfigReplaceEndpoints.yml" ,
90+ config : HardenRunnerConfig {Config : blockConfig , Subtractive : true },
91+ wantUpdated : true ,
92+ outputFile : "updateConfigReplaceEndpoints.yml" ,
93+ },
94+ {
95+ name : "subtractive replaces config with comments" ,
96+ inputFile : "updateConfigWithComments.yml" ,
97+ config : HardenRunnerConfig {Config : blockConfig , Subtractive : true },
98+ wantUpdated : true ,
99+ outputFile : "updateConfigWithComments.yml" ,
100+ },
101+ {
102+ name : "subtractive replaces single-line allowed-endpoints" ,
103+ inputFile : "updateConfigSingleLine.yml" ,
104+ config : HardenRunnerConfig {Config : blockConfig , Subtractive : true },
105+ wantUpdated : true ,
106+ outputFile : "updateConfigSingleLine.yml" ,
107+ },
108+ {
109+ name : "subtractive with comments in config" ,
110+ inputFile : "updateConfigWithConfigComments.yml" ,
111+ config : HardenRunnerConfig {Config : blockConfigWithComments , Subtractive : true },
112+ wantUpdated : true ,
113+ outputFile : "updateConfigWithConfigComments.yml" ,
114+ },
87115 }
88116
89117 for _ , tt := range tests {
90118 t .Run (tt .name , func (t * testing.T ) {
119+ input , err := ioutil .ReadFile (path .Join (inputDirectory , tt .inputFile ))
120+ if err != nil {
121+ t .Fatalf ("error reading input file: %v" , err )
122+ }
91123 got , gotUpdated , err := AddAction (string (input ), tt .config , false , false , false )
92124 if err != nil {
93125 t .Errorf ("AddAction() error = %v" , err )
0 commit comments