github.com/hashicorp/packer@v1.14.3/provisioner/sleep/provisioner_test.go (about)

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: BUSL-1.1
     3  
     4  package sleep
     5  
     6  import (
     7  	"context"
     8  	"testing"
     9  	"time"
    10  )
    11  
    12  func test1sConfig() map[string]interface{} {
    13  	return map[string]interface{}{
    14  		"duration": "1s",
    15  	}
    16  }
    17  
    18  func TestConfigPrepare_1s(t *testing.T) {
    19  	raw := test1sConfig()
    20  	var p Provisioner
    21  	err := p.Prepare(raw)
    22  	if err != nil {
    23  		t.Fatalf("prerare failed: %v", err)
    24  	}
    25  
    26  	if p.Duration != time.Second {
    27  		t.Fatal("wrong duration")
    28  	}
    29  }
    30  
    31  func TestProvisioner_Provision(t *testing.T) {
    32  	ctxCancelled, cancel := context.WithCancel(context.Background())
    33  	cancel()
    34  	type fields struct {
    35  		Duration time.Duration
    36  	}
    37  	type args struct {
    38  		ctx context.Context
    39  	}
    40  	tests := []struct {
    41  		name    string
    42  		fields  fields
    43  		args    args
    44  		wantErr bool
    45  	}{
    46  		{"valid sleep", fields{1 * time.Millisecond}, args{context.Background()}, false},
    47  		{"timeout", fields{1 * time.Millisecond}, args{ctxCancelled}, true},
    48  	}
    49  	for _, tt := range tests {
    50  		t.Run(tt.name, func(t *testing.T) {
    51  			p := &Provisioner{
    52  				Duration: tt.fields.Duration,
    53  			}
    54  			if err := p.Provision(tt.args.ctx, nil, nil, make(map[string]interface{})); (err != nil) != tt.wantErr {
    55  				t.Errorf("Provisioner.Provision() error = %v, wantErr %v", err, tt.wantErr)
    56  			}
    57  		})
    58  	}
    59  }