github.com/grailbio/base@v0.0.11/cloud/awssession/provider_test.go (about)

     1  // Copyright 2018 GRAIL, Inc. All rights reserved.
     2  // Use of this source code is governed by the Apache-2.0
     3  // license that can be found in the LICENSE file.
     4  
     5  package awssession
     6  
     7  import (
     8  	"testing"
     9  
    10  	"github.com/grailbio/base/security/ticket"
    11  )
    12  
    13  func TestProviderRetrieve(t *testing.T) {
    14  	noError := false
    15  	withError := true
    16  	notExpired := false
    17  	isExpired := true
    18  
    19  	cases := []struct {
    20  		description    string
    21  		ticket         ticket.Ticket
    22  		wantErr        bool
    23  		wantExpiration bool
    24  	}{
    25  		{"nil", nil, withError, isExpired},
    26  		{"empty AWS ticket", ticket.TicketAwsTicket{}, noError, notExpired},
    27  		{"empty expiration", ticket.TicketAwsTicket{
    28  			Value: ticket.AwsTicket{
    29  				AwsCredentials: ticket.AwsCredentials{
    30  					Expiration: "",
    31  				},
    32  			},
    33  		}, noError, notExpired},
    34  		{"bad expiration", ticket.TicketAwsTicket{
    35  			Value: ticket.AwsTicket{
    36  				AwsCredentials: ticket.AwsCredentials{
    37  					Expiration: "bad-expiration-text",
    38  				},
    39  			},
    40  		}, withError, isExpired},
    41  		{"valid expiration", ticket.TicketAwsTicket{
    42  			Value: ticket.AwsTicket{
    43  				AwsCredentials: ticket.AwsCredentials{
    44  					Expiration: "2018-01-01T00:00:00Z",
    45  				},
    46  			},
    47  		}, noError, isExpired},
    48  		{"valid non-expired expiration", ticket.TicketAwsTicket{
    49  			Value: ticket.AwsTicket{
    50  				AwsCredentials: ticket.AwsCredentials{
    51  					Expiration: "2100-01-01T00:00:00Z",
    52  				},
    53  			},
    54  		}, noError, notExpired},
    55  	}
    56  
    57  	for _, c := range cases {
    58  		p := Provider{Ticket: c.ticket}
    59  		_, gotErr := p.retrieve()
    60  		if gotErr != nil && !c.wantErr {
    61  			t.Errorf("%+v: got error %q, want error %v", c.description, gotErr, c.wantErr)
    62  		}
    63  		if gotExpiration := p.IsExpired(); gotExpiration != c.wantExpiration {
    64  			t.Errorf("%+v: got IsExpired() %v, want %v", c.description, gotExpiration, c.wantExpiration)
    65  		}
    66  	}
    67  }