github.com/Lephar/snapd@v0.0.0-20210825215435-c7fba9cef4d2/overlord/servicestate/servicestatetest/quotas.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2021 Canonical Ltd
     5   *
     6   * This program is free software: you can redistribute it and/or modify
     7   * it under the terms of the GNU General Public License version 3 as
     8   * published by the Free Software Foundation.
     9   *
    10   * This program is distributed in the hope that it will be useful,
    11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13   * GNU General Public License for more details.
    14   *
    15   * You should have received a copy of the GNU General Public License
    16   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17   *
    18   */
    19  
    20  package servicestatetest
    21  
    22  import (
    23  	"fmt"
    24  
    25  	"github.com/snapcore/snapd/gadget/quantity"
    26  	"github.com/snapcore/snapd/overlord/servicestate/internal"
    27  	"github.com/snapcore/snapd/overlord/state"
    28  	"github.com/snapcore/snapd/snap/quota"
    29  )
    30  
    31  // PatchQuotas will update the state quota group map with the provided quota
    32  // groups. It exposes internal.PatchQuotas for use in tests.
    33  func PatchQuotas(st *state.State, grps ...*quota.Group) (map[string]*quota.Group, error) {
    34  	return internal.PatchQuotas(st, grps...)
    35  }
    36  
    37  func MockQuotaInState(st *state.State, quotaName string, parentName string, snaps []string, memoryLimit quantity.Size) error {
    38  	allGrps, err := internal.AllQuotas(st)
    39  	if err != nil {
    40  		return nil
    41  	}
    42  
    43  	var parentGrp *quota.Group
    44  	if parentName != "" {
    45  		var ok bool
    46  		parentGrp, ok = allGrps[parentName]
    47  		if !ok {
    48  			return fmt.Errorf("cannot use non-existing quota group %q as parent group for %q", parentName, quotaName)
    49  		}
    50  	}
    51  
    52  	_, _, err = internal.CreateQuotaInState(st, quotaName, parentGrp, snaps, memoryLimit, allGrps)
    53  	return err
    54  }