From patchwork Thu Mar 30 11:08:03 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tobias Schaffner X-Patchwork-Id: 2752 Return-Path: Received: from shymkent.ilbers.de ([unix socket]) by shymkent (Cyrus 2.5.10-Debian-2.5.10-3+deb9u2) with LMTPA; Thu, 30 Mar 2023 13:09:02 +0200 X-Sieve: CMU Sieve 2.4 Received: from mail-lj1-f186.google.com (mail-lj1-f186.google.com [209.85.208.186]) by shymkent.ilbers.de (8.15.2/8.15.2/Debian-8+deb9u1) with ESMTPS id 32UB929m008116 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 30 Mar 2023 13:09:02 +0200 Received: by mail-lj1-f186.google.com with SMTP id s28-20020a05651c049c00b002a3e6d1594bsf3946655ljc.21; Thu, 30 Mar 2023 04:09:02 -0700 (PDT) ARC-Seal: i=3; a=rsa-sha256; t=1680174536; cv=pass; d=google.com; s=arc-20160816; b=XVrQzdQQmQpVwCO99voivsTW+qiz8de4vWhgORNT3FFGOJWSKYwW0FKodK+uidTOlT IuKjiFTETgItcLEB9pofjtbDcl3K24EneBUCsFiWEfj//ViJMdqPsA8BphkPUr22I6Xq wYlioM2SVEzYoDXIYg8SzIQY/I0pU4y6fY+CH5JoipwSiqEjULhiQqTuc63AMr09KNrq oGc4rZSnkbmDMinq5Uj8fpqCJ4cA/oI2B82TKLPG4s8FJdAcQI4E9VS72SFgITpc3LjK cJFBi8ANTEaYeSyJib1XtAyej4tMFNr+SvBMEq6PGWftjdBVS6t4xRashVWwyGRyJz8i qXmw== ARC-Message-Signature: i=3; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-unsubscribe:list-subscribe:list-archive:list-help:list-post :list-id:mailing-list:precedence:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:dkim-signature; bh=6M1G4P3KgcPiLNiQbTIeUXbZtiQkqq4/gPypkdMk9VM=; b=FoRfc4uYZ/A0dM3H2Rxx0WwSXAtxaX91W6WdHQ3NvD9thDIqzXaJO640QtKEJY7ze/ Xdox45U7r0VO31X56pg4/1rwBomDnYNDEfYog5P7XsvRiqwDvh9bzcrqFoNAVaqCVNNI Q+4EAozVBek4haJRb3NHq27/NPQrPTqUfdKFAHy65RoFGWt3a0C6wdIqhRiJSkmEWkm0 30By5XmXsYxyuRTKEFisj5Zkz4CCOpItTtZiiIr3CxHNAzSjmGu7CmLMbR5Eb6y9S9KS z5uW04E68b6TxgMiJHWIlE3HTfNkZ6iB95zuW9/Fu30soHU6F3zuqmaOBZuPQclT8LMT gueQ== ARC-Authentication-Results: i=3; gmr-mx.google.com; dkim=pass header.i=@siemens.com header.s=selector2 header.b=zNbg5xaG; arc=pass (i=1 spf=pass spfdomain=siemens.com dmarc=pass fromdomain=siemens.com); spf=pass (google.com: domain of tobias.schaffner@siemens.com designates 2a01:111:f400:7e1b::62b as permitted sender) smtp.mailfrom=tobias.schaffner@siemens.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=siemens.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlegroups.com; s=20210112; t=1680174536; h=list-unsubscribe:list-subscribe:list-archive:list-help:list-post :list-id:mailing-list:precedence:x-original-authentication-results :x-original-sender:mime-version:references:in-reply-to:message-id :date:subject:cc:to:from:sender:from:to:cc:subject:date:message-id :reply-to; bh=6M1G4P3KgcPiLNiQbTIeUXbZtiQkqq4/gPypkdMk9VM=; b=BcC0mx2ikjPnJKfaBIYtxQb4TDZtouFrCQXBZtkJHJ/m1JrScItULloFBXgZfunYrr 3aC5veb8QimQW9gh+dMwRAv4uRuO4w8NY9lMpJTW0oL2cBmA4gIPWhVnTWOrD2TdjnW/ pfw+EHAZNwDwa/8R9bVWj228cLFkb77o8pjiJ2DrRlpKiT9A6Kvc6Vosyump6SDrj3eb aKz6pOMNz6D2kRH4u/XCrMb8vccDAewR5ylSM2k2IBRK5nhlEqVzL6bdxq7CsZII9UgO r0EYUjWzy0PI01IyHybhAd9bEGIm4W4HnmLl5/7JmX947ZoZh2XFUrW2Ym/cRk/D2ruD Zbpg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680174536; h=list-unsubscribe:list-subscribe:list-archive:list-help:list-post :x-spam-checked-in-group:list-id:mailing-list:precedence :x-original-authentication-results:x-original-sender:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :x-gm-message-state:sender:from:to:cc:subject:date:message-id :reply-to; bh=6M1G4P3KgcPiLNiQbTIeUXbZtiQkqq4/gPypkdMk9VM=; b=3f7rqdOiXK+HU83mUKGG6OEIda6wq2scDivFyTAKricHwSdptQP1/p4haP2yCt+jx1 Lh7wMQYNrpLf5GmLSkc7KkUl3hG3pKVSN3fkJ3eEqBwOvFaz+6IGx5t9VNsfhVBrVi9o 8EppL0qH5ntSTi0Qs//hYVnBJ1XCC5JPn6wrFaH2VEUzHagatS/jHIBOEKDCrL+4oLn3 Yi/Bce/zIjyoR43nIPeGDTKvwoCxstc6u09ropPOIoHG7zdaGRAI2jYY193zeRKwg2TM BmuYdMNHEvXB8O2rcOzPwXnp0PGU8fewmMUX95bIZRcnV4cKDYXvR6icL8WAkzMTpb9y yuoQ== Sender: isar-users@googlegroups.com X-Gm-Message-State: AAQBX9d7YvMvL0gT6TCDcT2aUboKLFiP3w/CdFAInMVHjG+EF+aA8Ymv YbI3ozJ76grKLnAn/jbFKDw= X-Google-Smtp-Source: AKy350ZWf3cF5G3LtttUHRBqQXMSoEvw4XOHWroDI6aWQMIJ8L1PlV5VHS/znjV2ePFrmgUDFl2XIA== X-Received: by 2002:a05:6512:41b:b0:4e9:74fe:91b8 with SMTP id u27-20020a056512041b00b004e974fe91b8mr6583995lfk.6.1680174536473; Thu, 30 Mar 2023 04:08:56 -0700 (PDT) X-BeenThere: isar-users@googlegroups.com Received: by 2002:a2e:8e6f:0:b0:295:b83a:4ad6 with SMTP id t15-20020a2e8e6f000000b00295b83a4ad6ls320655ljk.3.-pod-prod-gmail; Thu, 30 Mar 2023 04:08:55 -0700 (PDT) X-Received: by 2002:a2e:6a1a:0:b0:295:b0cd:519 with SMTP id f26-20020a2e6a1a000000b00295b0cd0519mr6788295ljc.3.1680174535097; Thu, 30 Mar 2023 04:08:55 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1680174535; cv=pass; d=google.com; s=arc-20160816; b=WKKyPmMfK7FFBPXnEqK6ch3TOxvmMFxN0PsAkPq3K15zpaNC8IP0cpbYC0xX9Y8Bat OTomznRlBIuIVobXYRiKcS4BpedZQTD1hLvJNQ+pY1V1DtJWEepXjrzZ6zRCSIPGWVeZ N2sS2IZ3qPjPvJU+KmmaoMyWOwcZZ6Btq1X2FNJKiZqCViPcLS45PvMSCMtBoRiabR2x OyUcYdF/QoxRlfw6/snP5SLZB/goQN9mO0bUXEgNTUK/fIff1iYkp7M4WKxJiQVywlKt uGt+I2C4lXpoSIXzxzC6iIy91QD0+ThHAlU8xlYwm4eJWXQB72Ozy6vjvV6cO2/FjaBV ofXQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature; bh=M0Ltgm50tz0ySrE2rBdSa+pfRmGCF0qe7g9yPN8+VHc=; b=YwmZwCv/ChOfDVDxRNwhGWrwmWx67zZKfpooKrWhvX0GTcGFaApSAHYiTiV6xd8XDE Z8aAhePcA65UvPnanij1JTkkAW4P7GKBJ8qStivA/1wyqrPhAJdIUaWAoff/40psCx3y tYB1hf0xRRt4tGble7x+XV4+ijp9zn0/ffKUfEo77TYUM3aXAurTqoXGCVd2nKhgpyB0 FwfwhJplnbhp6SExMf21rII7A0rR4+GU3m+NgD7TRfGp07Oorys/hJhJwJf9IwNMqk0c gyfiDZkxmXr78e49mT0C1AZ3bSGxfH6fTUij1cgVgZHN/neAPoKh8stZTgXA14l8vguZ y0JQ== ARC-Authentication-Results: i=2; gmr-mx.google.com; dkim=pass header.i=@siemens.com header.s=selector2 header.b=zNbg5xaG; arc=pass (i=1 spf=pass spfdomain=siemens.com dmarc=pass fromdomain=siemens.com); spf=pass (google.com: domain of tobias.schaffner@siemens.com designates 2a01:111:f400:7e1b::62b as permitted sender) smtp.mailfrom=tobias.schaffner@siemens.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=siemens.com Received: from EUR05-AM6-obe.outbound.protection.outlook.com (mail-am6eur05on2062b.outbound.protection.outlook.com. [2a01:111:f400:7e1b::62b]) by gmr-mx.google.com with ESMTPS id l6-20020a2ea806000000b002a5fd844b80si497030ljq.1.2023.03.30.04.08.54 for (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 30 Mar 2023 04:08:55 -0700 (PDT) Received-SPF: pass (google.com: domain of tobias.schaffner@siemens.com designates 2a01:111:f400:7e1b::62b as permitted sender) client-ip=2a01:111:f400:7e1b::62b; ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=D2cefKstkGvWPvY7RFy6Z+f0eP1n2bBksH82H/wXafeEgxt8mEJcu3JmjXEWXbiln5PGoTzwQWvrnfUT/oOWhIulxYqAhr1GYCimaiIzsVGLIQUyiuUly84ZPz9eRdHUmR+UGGsGhn6BKzq8CSWtA3V/ZJf76f1jlHyfL38GRrmddtM5EtlsZvN5aFDJCKo64sJv3t0W5vtIsCjv1Gj18vB8zH8xsCr9ZY/gC7gBUVVxFjOce1QI7qKlUli6K2CySFWm37qzdzAGS+Hs9EbxEYfMtMsdYKpqyOBTDs8KXbEYAYE/zQyk2DJvYeZDdAGB4qAFl0ctfYKVSKtq28cg5Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=M0Ltgm50tz0ySrE2rBdSa+pfRmGCF0qe7g9yPN8+VHc=; b=J3g+CYgko+Y8AZKsAG7BzY4pWiEFosX5N8gaOZQWBiFFmKvfVmuxmJx9JYISsLclvDNzQT8h0tYE6pxQ5j2Mo+GfxINj1n7NKMqHU54VyQ+8dALj/OFS2goD7Mc+PZlsiS9yT4djPTuv63EeNtiipErMVrPlW/9LyppB5ZCgDrewVHq76JKUXl8upV9FDgE4s4zWKijO31c0gfmRFMZy/S5EMSZTQBl3TZX4oJfZgY39RCHPBPbd3WjKwGgZ3pr+9k5K4+QWT1jPY4N8z+GUaSSGDPCAy2yfDOsEXT7vsfFNddRvyrGZcgKUbNIPrP5HjT7JDre6yPnPlDewN01oRw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 194.138.21.75) smtp.rcpttodomain=googlegroups.com smtp.mailfrom=siemens.com; dmarc=pass (p=none sp=none pct=100) action=none header.from=siemens.com; dkim=none (message not signed); arc=none Received: from GV3P280CA0050.SWEP280.PROD.OUTLOOK.COM (2603:10a6:150:9::13) by PAWPR10MB8258.EURPRD10.PROD.OUTLOOK.COM (2603:10a6:102:388::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6254.18; Thu, 30 Mar 2023 11:08:53 +0000 Received: from HE1EUR01FT102.eop-EUR01.prod.protection.outlook.com (2603:10a6:150:9:cafe::78) by GV3P280CA0050.outlook.office365.com (2603:10a6:150:9::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6254.22 via Frontend Transport; Thu, 30 Mar 2023 11:08:53 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 194.138.21.75) smtp.mailfrom=siemens.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=siemens.com; Received-SPF: Pass (protection.outlook.com: domain of siemens.com designates 194.138.21.75 as permitted sender) receiver=protection.outlook.com; client-ip=194.138.21.75; helo=hybrid.siemens.com; pr=C Received: from hybrid.siemens.com (194.138.21.75) by HE1EUR01FT102.mail.protection.outlook.com (10.152.1.64) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6254.22 via Frontend Transport; Thu, 30 Mar 2023 11:08:53 +0000 Received: from DEMCHDC8WBA.ad011.siemens.net (139.25.226.105) by DEMCHDC8VRA.ad011.siemens.net (194.138.21.75) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.25; Thu, 30 Mar 2023 13:08:52 +0200 Received: from L15-Gen2.fritz.box (139.25.0.85) by DEMCHDC8WBA.ad011.siemens.net (139.25.226.105) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.25; Thu, 30 Mar 2023 13:08:52 +0200 From: "T. Schaffner" To: CC: , , "Tobias Schaffner" Subject: [PATCH 3/4] add unittests for the image-account-extension Date: Thu, 30 Mar 2023 13:08:03 +0200 Message-ID: <20230330110804.1016614-4-tobias.schaffner@siemens.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230330110804.1016614-1-tobias.schaffner@siemens.com> References: <20230330110804.1016614-1-tobias.schaffner@siemens.com> MIME-Version: 1.0 X-Originating-IP: [139.25.0.85] X-ClientProxiedBy: DEMCHDC8WAA.ad011.siemens.net (139.25.226.104) To DEMCHDC8WBA.ad011.siemens.net (139.25.226.105) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: HE1EUR01FT102:EE_|PAWPR10MB8258:EE_ X-MS-Office365-Filtering-Correlation-Id: d6785e18-7d8f-48e8-86ab-08db310f2613 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 32U7i/cs0Js1KdYyC2IJ2LdHhNK2O44kUnERpcSrnS8l9Z5UgMjhlsEoTOeJwsAl5+ZHjCXfqw7XzXocc5fjBdTl3X8pnU0gr7RWARFEMedtHbBJyvyPB+2B2vi1nqdP3frkukPecHwCrnro8DY+rFg2Gm/UIbHmauClXn2ijbi90iJOq9O/+v8hh7gSaPJ6ggHHqf8891ZCUhm/JWJjSGjQ9SLvzDTgQVipX69wdOR1ViLon7+2NhWd8HmA1Yvrul7aUvQsq/RwPljqtWhQbWZ6bpoqPumhXn0pIYtv5Tamixy8kNVSldEZ1y/s/lEdkkJe7vuJ9rsjMCpb0Fg8fkT+pyjKsz7ApOnJ5v/jZ4kERXLBq0BMHt7tRvah8NeWgfYk9GMl0MGOBOQizCgQtEgvF4QmpuUHjYdSlGzQyfxc/VI9IldT4lmqVIV63iEFnCl5SDR210GM4LwjVEia1Cn3cTY3UNr5gBldebUijtRlAKknPser/GHib34l+PmO7k/5NJHAWwYhS2GoFoJKuqvuOM9NB+fPQ0SrO0iSw2WxH4pefb/pkAG2UmY2YS/3Kz70WE5+DfnS3w75/n+KqE81CRaSqPyW3TSrJtti/pQXwaVvVLixR/Q2xZZkQNXckQMqcMKkAC+E9c4XoAVjf7ZJrLqrdIp1L5wprhHuNseibVY/cqHhCUajS1rHSFCsTntmTAD5TYkHy9JfmILqRC26eLKg/OemGjuf08thgrmLqKkgOc9FYI4S8uTjbkLVy6/yjaHMBbxQPjuu3iGsYA== X-Forefront-Antispam-Report: CIP:194.138.21.75;CTRY:DE;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:hybrid.siemens.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230028)(4636009)(39860400002)(396003)(346002)(136003)(376002)(451199021)(46966006)(36840700001)(40470700004)(82310400005)(81166007)(40460700003)(34020700004)(36860700001)(316002)(86362001)(82960400001)(478600001)(356005)(82740400003)(54906003)(6666004)(15650500001)(16526019)(107886003)(956004)(2616005)(47076005)(36756003)(1076003)(8936002)(26005)(40480700001)(336012)(2906002)(83380400001)(70586007)(70206006)(41300700001)(5660300002)(8676002)(186003)(6916009)(4326008)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: siemens.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 30 Mar 2023 11:08:53.5148 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: d6785e18-7d8f-48e8-86ab-08db310f2613 X-MS-Exchange-CrossTenant-Id: 38ae3bcd-9579-4fd4-adda-b42e1495d55a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=38ae3bcd-9579-4fd4-adda-b42e1495d55a;Ip=[194.138.21.75];Helo=[hybrid.siemens.com] X-MS-Exchange-CrossTenant-AuthSource: HE1EUR01FT102.eop-EUR01.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: PAWPR10MB8258 X-Original-Sender: tobias.schaffner@siemens.com X-Original-Authentication-Results: gmr-mx.google.com; dkim=pass header.i=@siemens.com header.s=selector2 header.b=zNbg5xaG; arc=pass (i=1 spf=pass spfdomain=siemens.com dmarc=pass fromdomain=siemens.com); spf=pass (google.com: domain of tobias.schaffner@siemens.com designates 2a01:111:f400:7e1b::62b as permitted sender) smtp.mailfrom=tobias.schaffner@siemens.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=siemens.com Precedence: list Mailing-list: list isar-users@googlegroups.com; contact isar-users+owners@googlegroups.com List-ID: X-Spam-Checked-In-Group: isar-users@googlegroups.com X-Google-Group-Id: 914930254986 List-Post: , List-Help: , List-Archive: , List-Unsubscribe: , X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_EF,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RCVD_IN_MSPIKE_H2,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on shymkent.ilbers.de X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= From: Tobias Schaffner This is a first example on how to use the unittest_isar module to test python functions defined in a bitbake file. Signed-off-by: Tobias Schaffner --- .../unittests/test_image_account_extension.py | 145 ++++++++++++++++++ 1 file changed, 145 insertions(+) create mode 100644 testsuite/unittests/test_image_account_extension.py diff --git a/testsuite/unittests/test_image_account_extension.py b/testsuite/unittests/test_image_account_extension.py new file mode 100644 index 00000000..d35de723 --- /dev/null +++ b/testsuite/unittests/test_image_account_extension.py @@ -0,0 +1,145 @@ +# This software is a part of ISAR. +# Copyright (C) Siemens AG, 2023 +# +# SPDX-License-Identifier: MIT + +from bitbake import load_function, DataSmart +from rootfs import TemporaryRootfs + +import unittest +from unittest.mock import patch +from typing import Tuple + + +file_name = "meta/classes/image-account-extension.bbclass" +image_create_users = load_function(file_name, "image_create_users") +image_create_groups = load_function(file_name, "image_create_groups") + + +class TestImageAccountExtensionCommon(unittest.TestCase): + + def setup(self) -> Tuple[DataSmart, TemporaryRootfs]: + rootfs = TemporaryRootfs() + + d = DataSmart() + d.setVar("ROOTFSDIR", rootfs.path()) + + return (d, rootfs) + + +class TestImageAccountExtensionImageCreateUsers(TestImageAccountExtensionCommon): + + def setup(self, user_name: str) -> Tuple[DataSmart, TemporaryRootfs]: + d, rootfs = super().setup() + rootfs.create_file( + "/etc/passwd", "test:x:1000:1000::/home/test:/bin/sh") + d.setVar("USERS", user_name) + return (d, rootfs) + + def test_new_user(self): + test_user = "new" + d, rootfs = self.setup(test_user) + + with patch.object(bb.process, "run") as run_mock: + image_create_users(d) + + run_mock.assert_called_once_with( + ["sudo", "-E", "chroot", rootfs.path(), "/usr/sbin/useradd", test_user]) + + def test_existing_user_no_change(self): + test_user = "test" + d, _ = self.setup(test_user) + + with patch.object(bb.process, "run") as run_mock: + image_create_users(d) + + run_mock.assert_not_called() + + def test_existing_user_home_change(self): + test_user = "test" + d, _ = self.setup(test_user) + d.setVarFlag("USER_{}".format(test_user), "home", "/home/new_home") + + with patch.object(bb.process, "run") as run_mock: + image_create_users(d) + + assert run_mock.call_count == 1 + assert run_mock.call_args[0][0][-5:] == ["/usr/sbin/usermod", + '--home', '/home/new_home', '--move-home', 'test'] + + def test_deterministic_password(self): + test_user = "new" + cleartext_password = "test" + d, _ = self.setup(test_user) + + d.setVarFlag("USER_{}".format(test_user), + "flags", "clear-text-password") + d.setVarFlag("USER_{}".format(test_user), + "password", cleartext_password) + + source_date_epoch = "1672427776" + d.setVar("SOURCE_DATE_EPOCH", source_date_epoch) + + # openssl passwd -6 -salt $(echo "1672427776" | sha256sum -z | cut -c 1-15) test + encrypted_password = "$6$eb2e2a12cccc88a$IuhgisFe5AKM5.VREKg8wIAcPSkaJDWBM1cMUsEjNZh2Wa6BT2f5OFhqGTGpL4lFzHGN8oiwvAh0jFO1GhO3S." + + with patch.object(bb.process, "run") as run_mock: + image_create_users(d) + + assert run_mock.call_count == 2 + assert run_mock.call_args[0][1] == "{}:{}".format( + test_user, encrypted_password).encode() + + +class TestImageAccountExtensionImageCreateGroups(TestImageAccountExtensionCommon): + + def setup(self, group_name: str) -> Tuple[DataSmart, TemporaryRootfs]: + d, rootfs = super().setup() + rootfs.create_file("/etc/group", "test:x:1000:test") + d.setVar("GROUPS", group_name) + return (d, rootfs) + + def test_new_group(self): + test_group = "new" + d, rootfs = self.setup(test_group) + + with patch.object(bb.process, "run") as run_mock: + image_create_groups(d) + + run_mock.assert_called_once_with( + ["sudo", "-E", "chroot", rootfs.path(), "/usr/sbin/groupadd", test_group]) + + def test_existing_group_no_change(self): + test_group = "test" + d, _ = self.setup(test_group) + + with patch.object(bb.process, "run") as run_mock: + image_create_groups(d) + + run_mock.assert_not_called() + + def test_existing_group_id_change(self): + test_group = "test" + d, rootfs = self.setup(test_group) + d.setVarFlag("GROUP_{}".format(test_group), "gid", "1005") + + with patch.object(bb.process, "run") as run_mock: + image_create_groups(d) + + run_mock.assert_called_once_with( + ["sudo", "-E", "chroot", rootfs.path(), "/usr/sbin/groupmod", "--gid", "1005", test_group]) + + def test_system_flag(self): + test_group = "test" + d, _ = self.setup(test_group) + d.setVarFlag("GROUP_{}".format(test_group), "flags", "system") + + with patch.object(bb.process, "run") as run_mock: + image_create_groups(d) + + assert run_mock.call_count == 1 + assert "--system" in run_mock.call_args[0][0] + + +if __name__ == "__main__": + unittest.main()