go.fuchsia.dev/infra@v0.0.0-20240507153436-9b593402251b/cmd/size_diff/diff/diff_test.go (about)

     1  // Copyright 2021 The Fuchsia Authors.
     2  // Use of this source code is governed by a BSD-style license that can be
     3  // found in the LICENSE file.
     4  
     5  package diff
     6  
     7  import (
     8  	"sort"
     9  	"testing"
    10  
    11  	"github.com/google/go-cmp/cmp"
    12  	"go.fuchsia.dev/infra/cmd/size_check/sizes"
    13  )
    14  
    15  func TestDiffBinarySizes(t *testing.T) {
    16  	t.Parallel()
    17  
    18  	tests := []struct {
    19  		name                string
    20  		baselineBinarySizes sizes.BinarySizes
    21  		binarySizes         sizes.BinarySizes
    22  		expected            *Diff
    23  	}{
    24  		{
    25  			name: "under budgets",
    26  			baselineBinarySizes: sizes.BinarySizes{
    27  				"componentA": {
    28  					Size: 25,
    29  					// These should be ignored. The budgets are determined by
    30  					// binarySizes.
    31  					Budget:      1,
    32  					CreepBudget: 1,
    33  				},
    34  				// componentB should be ignored completely. We only care about
    35  				// components in binarySizes.
    36  				"componentB": {
    37  					Size:        25,
    38  					Budget:      1,
    39  					CreepBudget: 1,
    40  				},
    41  			},
    42  			binarySizes: sizes.BinarySizes{
    43  				"componentA": {
    44  					Size:        30,
    45  					Budget:      50,
    46  					CreepBudget: 10,
    47  				},
    48  			},
    49  			expected: &Diff{
    50  				ComponentDiffs: []*ComponentDiff{
    51  					{
    52  						Name:                "componentA",
    53  						BaselineSize:        25,
    54  						Size:                30,
    55  						SizeDiff:            5,
    56  						Budget:              50,
    57  						CreepBudget:         10,
    58  						BudgetExceeded:      false,
    59  						CreepBudgetExceeded: false,
    60  					},
    61  				},
    62  				BudgetExceeded:      false,
    63  				CreepBudgetExceeded: false,
    64  			},
    65  		},
    66  		{
    67  			name: "over creep budget",
    68  			baselineBinarySizes: sizes.BinarySizes{
    69  				"componentA": {
    70  					Size:        25,
    71  					Budget:      1,
    72  					CreepBudget: 1,
    73  				},
    74  				"componentB": {
    75  					Size:        25,
    76  					Budget:      1,
    77  					CreepBudget: 1,
    78  				},
    79  			},
    80  			binarySizes: sizes.BinarySizes{
    81  				"componentA": {
    82  					Size:        30,
    83  					Budget:      50,
    84  					CreepBudget: 1,
    85  				},
    86  			},
    87  			expected: &Diff{
    88  				ComponentDiffs: []*ComponentDiff{
    89  					{
    90  						Name:                "componentA",
    91  						BaselineSize:        25,
    92  						Size:                30,
    93  						SizeDiff:            5,
    94  						Budget:              50,
    95  						CreepBudget:         1,
    96  						BudgetExceeded:      false,
    97  						CreepBudgetExceeded: true,
    98  					},
    99  				},
   100  				BudgetExceeded:      false,
   101  				CreepBudgetExceeded: true,
   102  			},
   103  		},
   104  		{
   105  			name: "skipped pairings",
   106  			baselineBinarySizes: sizes.BinarySizes{
   107  				"componentA": {
   108  					Size:        25,
   109  					Budget:      1,
   110  					CreepBudget: 1,
   111  				},
   112  				"componentB": {
   113  					Size:        25,
   114  					Budget:      1,
   115  					CreepBudget: 1,
   116  				},
   117  			},
   118  			binarySizes: sizes.BinarySizes{
   119  				"componentA": {
   120  					Size:        30,
   121  					Budget:      50,
   122  					CreepBudget: 10,
   123  				},
   124  				"componentC": {
   125  					Size:        30,
   126  					Budget:      50,
   127  					CreepBudget: 10,
   128  				},
   129  			},
   130  			expected: &Diff{
   131  				ComponentDiffs: []*ComponentDiff{
   132  					{
   133  						Name:                "componentC",
   134  						BaselineSize:        0,
   135  						Size:                30,
   136  						SizeDiff:            30,
   137  						Budget:              50,
   138  						CreepBudget:         10,
   139  						BudgetExceeded:      false,
   140  						CreepBudgetExceeded: false,
   141  					},
   142  					{
   143  						Name:                "componentA",
   144  						BaselineSize:        25,
   145  						Size:                30,
   146  						SizeDiff:            5,
   147  						Budget:              50,
   148  						CreepBudget:         10,
   149  						BudgetExceeded:      false,
   150  						CreepBudgetExceeded: false,
   151  					},
   152  				},
   153  				BudgetExceeded:      false,
   154  				CreepBudgetExceeded: false,
   155  			},
   156  		},
   157  	}
   158  
   159  	for _, test := range tests {
   160  		t.Run(test.name, func(t *testing.T) {
   161  			d := DiffBinarySizes(test.binarySizes, test.baselineBinarySizes)
   162  			sort.SliceStable(test.expected.ComponentDiffs, func(i, j int) bool {
   163  				return test.expected.ComponentDiffs[i].Name < test.expected.ComponentDiffs[j].Name
   164  			})
   165  			sort.SliceStable(d.ComponentDiffs, func(i, j int) bool {
   166  				return d.ComponentDiffs[i].Name < d.ComponentDiffs[j].Name
   167  			})
   168  			if diff := cmp.Diff(test.expected, d); diff != "" {
   169  				t.Fatalf("different (-want +got):\n%s", diff)
   170  			}
   171  		})
   172  	}
   173  }
   174  
   175  func TestNewDiff(t *testing.T) {
   176  	t.Parallel()
   177  
   178  	componentDiffs := []*ComponentDiff{
   179  		{
   180  			Name:                "componentA",
   181  			BaselineSize:        25,
   182  			Size:                35,
   183  			SizeDiff:            10,
   184  			Budget:              30,
   185  			CreepBudget:         15,
   186  			BudgetExceeded:      true,
   187  			CreepBudgetExceeded: false,
   188  		},
   189  		{
   190  			Name:                "componentB",
   191  			BaselineSize:        25,
   192  			Size:                45,
   193  			SizeDiff:            20,
   194  			Budget:              50,
   195  			CreepBudget:         10,
   196  			BudgetExceeded:      false,
   197  			CreepBudgetExceeded: true,
   198  		},
   199  		{
   200  			Name:                "componentC",
   201  			BaselineSize:        25,
   202  			Size:                45,
   203  			SizeDiff:            20,
   204  			Budget:              40,
   205  			CreepBudget:         10,
   206  			BudgetExceeded:      true,
   207  			CreepBudgetExceeded: true,
   208  		},
   209  	}
   210  	tests := []struct {
   211  		name           string
   212  		componentDiffs []*ComponentDiff
   213  		expected       *Diff
   214  	}{
   215  		{
   216  			name:           "over absolute budget",
   217  			componentDiffs: componentDiffs[0:1],
   218  			expected: &Diff{
   219  				ComponentDiffs:      componentDiffs[0:1],
   220  				BudgetExceeded:      true,
   221  				CreepBudgetExceeded: false,
   222  			},
   223  		},
   224  		{
   225  			name:           "over creep budget",
   226  			componentDiffs: componentDiffs[1:2],
   227  			expected: &Diff{
   228  				ComponentDiffs:      componentDiffs[1:2],
   229  				BudgetExceeded:      false,
   230  				CreepBudgetExceeded: true,
   231  			},
   232  		},
   233  		{
   234  			name:           "over absolute and creep budgets",
   235  			componentDiffs: componentDiffs,
   236  			expected: &Diff{
   237  				ComponentDiffs:      componentDiffs,
   238  				BudgetExceeded:      true,
   239  				CreepBudgetExceeded: true,
   240  			},
   241  		},
   242  	}
   243  
   244  	for _, test := range tests {
   245  		t.Run(test.name, func(t *testing.T) {
   246  			d := NewDiff(test.componentDiffs)
   247  			sort.SliceStable(test.expected.ComponentDiffs, func(i, j int) bool {
   248  				return test.expected.ComponentDiffs[i].Name < test.expected.ComponentDiffs[j].Name
   249  			})
   250  			sort.SliceStable(d.ComponentDiffs, func(i, j int) bool {
   251  				return d.ComponentDiffs[i].Name < d.ComponentDiffs[j].Name
   252  			})
   253  			if diff := cmp.Diff(test.expected, d); diff != "" {
   254  				t.Fatalf("different (-want +got):\n%s", diff)
   255  			}
   256  		})
   257  	}
   258  }
   259  
   260  func TestNewComponentDiff(t *testing.T) {
   261  	t.Parallel()
   262  
   263  	tests := []struct {
   264  		name               string
   265  		componentName      string
   266  		baselineSize       int64
   267  		size               int64
   268  		budget             int64
   269  		creepBudget        int64
   270  		enforceCreepBudget bool
   271  		expected           *ComponentDiff
   272  	}{
   273  		{
   274  			name:               "under budgets",
   275  			componentName:      "componentA",
   276  			baselineSize:       25,
   277  			size:               30,
   278  			budget:             50,
   279  			creepBudget:        10,
   280  			enforceCreepBudget: true,
   281  			expected: &ComponentDiff{
   282  				Name:                "componentA",
   283  				BaselineSize:        25,
   284  				Size:                30,
   285  				SizeDiff:            5,
   286  				Budget:              50,
   287  				CreepBudget:         10,
   288  				BudgetExceeded:      false,
   289  				CreepBudgetExceeded: false,
   290  			},
   291  		},
   292  		{
   293  			name:               "over absolute budget",
   294  			componentName:      "componentA",
   295  			baselineSize:       25,
   296  			size:               35,
   297  			budget:             30,
   298  			creepBudget:        15,
   299  			enforceCreepBudget: true,
   300  			expected: &ComponentDiff{
   301  				Name:                "componentA",
   302  				BaselineSize:        25,
   303  				Size:                35,
   304  				SizeDiff:            10,
   305  				Budget:              30,
   306  				CreepBudget:         15,
   307  				BudgetExceeded:      true,
   308  				CreepBudgetExceeded: false,
   309  			},
   310  		},
   311  		{
   312  			name:               "over creep budget",
   313  			componentName:      "componentA",
   314  			baselineSize:       25,
   315  			size:               45,
   316  			budget:             50,
   317  			creepBudget:        10,
   318  			enforceCreepBudget: true,
   319  			expected: &ComponentDiff{
   320  				Name:                "componentA",
   321  				BaselineSize:        25,
   322  				Size:                45,
   323  				SizeDiff:            20,
   324  				Budget:              50,
   325  				CreepBudget:         10,
   326  				BudgetExceeded:      false,
   327  				CreepBudgetExceeded: true,
   328  			},
   329  		},
   330  		{
   331  			name:               "creep budget not enforced",
   332  			componentName:      "componentA",
   333  			baselineSize:       25,
   334  			size:               45,
   335  			budget:             50,
   336  			creepBudget:        10,
   337  			enforceCreepBudget: false,
   338  			expected: &ComponentDiff{
   339  				Name:                "componentA",
   340  				BaselineSize:        25,
   341  				Size:                45,
   342  				SizeDiff:            20,
   343  				Budget:              50,
   344  				CreepBudget:         10,
   345  				BudgetExceeded:      false,
   346  				CreepBudgetExceeded: false,
   347  			},
   348  		},
   349  		{
   350  			name:               "over absolute and creep budgets",
   351  			componentName:      "componentA",
   352  			baselineSize:       25,
   353  			size:               45,
   354  			budget:             40,
   355  			creepBudget:        10,
   356  			enforceCreepBudget: true,
   357  			expected: &ComponentDiff{
   358  				Name:                "componentA",
   359  				BaselineSize:        25,
   360  				Size:                45,
   361  				SizeDiff:            20,
   362  				Budget:              40,
   363  				CreepBudget:         10,
   364  				BudgetExceeded:      true,
   365  				CreepBudgetExceeded: true,
   366  			},
   367  		},
   368  	}
   369  
   370  	for _, test := range tests {
   371  		t.Run(test.name, func(t *testing.T) {
   372  			cd := NewComponentDiff(test.componentName, test.baselineSize, test.size, test.budget, test.creepBudget, test.enforceCreepBudget)
   373  			if diff := cmp.Diff(test.expected, cd); diff != "" {
   374  				t.Fatalf("different (-want +got):\n%s", diff)
   375  			}
   376  		})
   377  	}
   378  }