eintopf.info@v0.13.16/service/killswitch/testutil/service.go (about)

     1  // Copyright (C) 2022 The Eintopf authors
     2  //
     3  // This program is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU Affero General Public License as
     5  // published by the Free Software Foundation, either version 3 of the
     6  // License, or (at your option) any later version.
     7  //
     8  // This program is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  // GNU Affero General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU Affero General Public License
    14  // along with this program.  If not, see <https://www.gnu.org/licenses/>.
    15  
    16  package testutil
    17  
    18  import (
    19  	"context"
    20  	"testing"
    21  
    22  	"eintopf.info/service/killswitch"
    23  )
    24  
    25  func TestKillSwitchService(t *testing.T, store killswitch.Storer) {
    26  	service := killswitch.NewService(store, []string{"valid"})
    27  
    28  	err := service.TurnOff(context.Background(), "invalid")
    29  	if err != killswitch.ErrInvalidHash {
    30  		t.Errorf("service.TurnOff(ctx, 'invalid'): hash should be invalid: error: %s", err)
    31  	}
    32  	err = service.TurnOff(context.Background(), "valid")
    33  	if err != nil {
    34  		t.Errorf("service.TurnOff(ctx, 'valid'): %s", err)
    35  	}
    36  
    37  	on, err := service.On(context.Background())
    38  	if err != nil {
    39  		t.Errorf("service.On(ctx): %s", err)
    40  	}
    41  	if on == false {
    42  		t.Errorf("service state should be off after succesful turn off")
    43  	}
    44  
    45  	err = service.TurnOn(context.Background(), "invalid")
    46  	if err != killswitch.ErrInvalidHash {
    47  		t.Errorf("service.TurnOn(ctx, 'invalid'): hash should be invalid: error: %s", err)
    48  	}
    49  	err = service.TurnOn(context.Background(), "valid")
    50  	if err != nil {
    51  		t.Errorf("service.TurnOn(ctx, 'valid'): %s", err)
    52  	}
    53  
    54  	on, err = service.On(context.Background())
    55  	if err != nil {
    56  		t.Errorf("service.On(ctx): %s", err)
    57  	}
    58  	if on == true {
    59  		t.Errorf("service state should be on after succesful turn on")
    60  	}
    61  }