github.com/yogeshkumararora/slsa-github-generator@v1.10.1-0.20240520161934-11278bd5afb4/internal/builders/docker/pkg/common_test.go (about)

     1  // Copyright 2022 SLSA Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     https://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package pkg
    16  
    17  import (
    18  	"encoding/json"
    19  	"fmt"
    20  	"os"
    21  	"testing"
    22  
    23  	"github.com/google/go-cmp/cmp"
    24  	slsa1 "github.com/in-toto/in-toto-golang/in_toto/slsa_provenance/v1"
    25  )
    26  
    27  func Test_BuildDefinition(t *testing.T) {
    28  	path := "../testdata/build-definition.json"
    29  	got, err := loadBuildDefinitionFromFile(path)
    30  	if err != nil {
    31  		t.Fatalf("%v", err)
    32  	}
    33  
    34  	wantSource := slsa1.ResourceDescriptor{
    35  		URI:    "git+https://github.com/yogeshkumararora/slsa-github-generator@refs/heads/main",
    36  		Digest: map[string]string{"sha1": "cf5804b5c6f1a4b2a0b03401a487dfdfbe3a5f00"},
    37  	}
    38  
    39  	wantBuilderImage := slsa1.ResourceDescriptor{
    40  		URI:    "bash@sha256:9e2ba52487d945504d250de186cb4fe2e3ba023ed2921dd6ac8b97ed43e76af9",
    41  		Digest: map[string]string{"sha256": "9e2ba52487d945504d250de186cb4fe2e3ba023ed2921dd6ac8b97ed43e76af9"},
    42  	}
    43  
    44  	want := &slsa1.ProvenanceBuildDefinition{
    45  		BuildType: "https://slsa.dev/container-based-build/v0.1?draft",
    46  		ExternalParameters: ContainerBasedExternalParameters{
    47  			Source:       wantSource,
    48  			BuilderImage: wantBuilderImage,
    49  			ConfigPath:   "internal/builders/docker/testdata/config.toml",
    50  			Config: BuildConfig{
    51  				ArtifactPath: "config.toml",
    52  				Command: []string{
    53  					"cp",
    54  					"internal/builders/docker/testdata/config.toml",
    55  					"config.toml",
    56  				},
    57  			},
    58  		},
    59  		ResolvedDependencies: []slsa1.ResourceDescriptor{
    60  			wantSource,
    61  		},
    62  	}
    63  
    64  	if diff := cmp.Diff(got, want); diff != "" {
    65  		t.Errorf(diff)
    66  	}
    67  }
    68  
    69  func loadBuildDefinitionFromFile(path string) (*slsa1.ProvenanceBuildDefinition, error) {
    70  	bdBytes, err := os.ReadFile(path)
    71  	if err != nil {
    72  		return nil, fmt.Errorf("could not read the JSON file in %q: %w", path, err)
    73  	}
    74  
    75  	var bd slsa1.ProvenanceBuildDefinition
    76  	if err := json.Unmarshal(bdBytes, &bd); err != nil {
    77  		return nil, fmt.Errorf("could not unmarshal the JSON file in %q as a BuildDefinition: %w", path, err)
    78  	}
    79  
    80  	var result map[string]interface{}
    81  	if err := json.Unmarshal(bdBytes, &result); err != nil {
    82  		return nil, fmt.Errorf("could not unmarshal the JSON file in %q as a map[string]interface{}: %w", path, err)
    83  	}
    84  
    85  	ep, ok := result["externalParameters"]
    86  	if !ok {
    87  		return nil, fmt.Errorf("missing externalParameters in BuildDefinition")
    88  	}
    89  
    90  	epBytes, err := json.Marshal(ep)
    91  	if err != nil {
    92  		return nil, fmt.Errorf("could not marshal the external params in %q: %w", path, err)
    93  	}
    94  
    95  	var containerEp ContainerBasedExternalParameters
    96  	if err := json.Unmarshal(epBytes, &containerEp); err != nil {
    97  		return nil, fmt.Errorf("could not unmarshal the JSON file in %q as a ContainerBasedExternalParameters: %w", path, err)
    98  	}
    99  
   100  	bd.ExternalParameters = containerEp
   101  
   102  	return &bd, nil
   103  }