public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
From: "Andrew Fish" <afish@apple.com>
To: devel@edk2.groups.io, Mike Kinney <michael.d.kinney@intel.com>
Cc: "bret.barkelew@microsoft.com" <bret.barkelew@microsoft.com>,
	"rfc@edk2.groups.io" <rfc@edk2.groups.io>
Subject: Re: [edk2-devel] EDK2 Host-Based Unit Test RFC (Now with docs!)
Date: Wed, 04 Dec 2019 09:50:33 -0800	[thread overview]
Message-ID: <0F460E19-2A3B-4FEE-8B6F-BE65B9C5F1D4@apple.com> (raw)
In-Reply-To: <E92EE9817A31E24EB0585FDF735412F5B9E25DDA@ORSMSX113.amr.corp.intel.com>

[-- Attachment #1: Type: text/plain, Size: 12545 bytes --]

Mike,

I like and agree with your comments.

On the UnitTestPkg(s) dependency issue I think it would make sense to move as much as possible into the *Pkg/Test/ ( *Pkg/HostLibrary,  MdePkg/MdePkgTest.dsc, etc.) directory structure. It looks like for implementation specific tests we skip the Test directory and drop the UnitTest or FuzzTest directly into modules directory. Maybe we should follow the same pattern as *Pkg/Test and have a Test directory? This will help minimize the number of "reserved" directories we need for managing the testing. Have a standardized directory structure will make it easier to differentiate the code from tests while doing `git grep` or other forms of code spelunking. 

A Host-Based Unit Test for a Library makes sense as you can link directly against the library and test it. A Host-Based Unit Test for Protocol/PPI/GUID [1] seems a little more complex. It is easy enough to write tests for the interfaces but what APIs do you call to get access to the protocol? 

Per our conversation at the Stewards meeting I think make sense to try to roll out the testing of libraries as the 1st phase. The mocking required for drivers could get quite complex and let us not get bogged down in all that to get something working. 

[1] *Pkg/Test/UnitTest/[Library|Protocol|Ppi|Guid] 

Thanks,

Andrew Fish


> On Dec 2, 2019, at 3:12 PM, Michael D Kinney <michael.d.kinney@intel.com> wrote:
> 
> Hi Bret,
>
> Thanks for posting this content.  Host based unit testing is a very valuable addition to the CI checks.
>
> I have the following comments:
>
> I see that MdePkg adds a dependency on UnitTestPkg.  This makes UnitTestPkg the root package when building and running host based unit tests.  This makes sense, but good to highlight that all packages that use host based tests will introduce a new package dependency on UnitTestPkg.
> Since the dependency only applies to unit tests, can we update the DependencyCheck plugin to support listing dependencies for FW components separate from dependencies for unit tests?
> I see UnitTestPkg declares 6 new lib classes.  Are all 6 classes intended to be used directly from a unit test case?  If some of these are only intended to be used from the framework inside the UnitTestPkg can we make them private?
> In the MdePkg, I see a new top level directory called ‘HostLibrary’. Since these lib instances are only used from a host based test, can they be moved into  the ‘Test’ directory?
> MdePkg/MdePkgTest.dsc. 
> Can this DSC file be moved into the ‘Test’ directory? 
> I see this DSC file only supports VS today.  How much work is it to add GCC support?
> MdePkg/HostLibrary/BaseLibHost
> Why are there 2 INFs.  One with ASM and one without ASM?  Can we just use the one with ASM and assume NASM is installed?
> I see the purpose of this lib instance is to call the
> SetJump()/LongJump().  So these implementations always work?  It looks like it assumes BASE_LIBRARY_JUMP_BUFFER is identical to the host OS user mode applicationsetjmp()/longjmp() state.
> Why are DRx and CRx registers emulated?  I would think and code that depends on read/writing these registers would not be compatible with host based testing.  Can we change to ASSERT (FALSE)?
> PatchInstructionX86() – I suspect this will not work in a host based environment because it is self modifying code.  Should it be ASSERT (FALSE)?
> MdePkg/HostLibrary/DebugLibHost
> What is ‘#ifndef TEST_WITH_KLEE’
> What is the ‘PatchFormat()’ function?  It is always disabled with if (0).
> Are the PCDs to set the debug message levels disabled on purpose? (DebugPrintEnabled(), DebugPrintLevelEnabled(), DebugCodeEnabled())
> MdePkg/HostLibrary/BaseMemoryLibHost
> Why can’t we use MdePkg/Library/BaseMemoryLib/BaseMemoryLib/inf instead and reduce the number of host specific lib instances?
> MdePkg/HostLibrary/MemoryAllocationLibHost
> Why is are memcpy(), assert(), memset() used in this lib?  I would expect this lib instance to match the UefiMemoryAllocationLib with the only the use of malloc() and free() to replace the UEFI Boot Services calls.
> Host library instance naming conventions.
> The current naming convention uses the environment as a prefix(e.g. Pei, Smm, Uefi) and the services the lib instance uses as a post fix.  Would it make more sense to use ‘Host’ as a prefix instead of a postfix in the lib instance names?
> Unicode vs ASCII strings
> I see InitUnitTestFramework(), CreateUnitTestSuite(), and AddTestCase() all take Unicode strings for the labels.  I also see extra code to convert gEfiCallerBaseName from ASCII to Unicode to use it as a short name of a test framework.  I think it would be simpler if the parameters to these APIs were ASCII and the framework can convert to Unicode if needed.
> Will InitUnitTestFramework() and CreateUnitTestSuite() always be called in pairs?  If so, can we combine these to a single API? 
> I see the SafeIntLib example create a single framework and multiple test suites.  Perhaps we can have a single CreateUnitTestSuite() API where Framework is an IN/OUT and if it is passed in as NULL, the Framework handle is created.
> I see a pattern where the CreateUnitTestSuite() ‘Package’ parameter is used as a prefix to every call to AddTestCase() in the ‘ClassName’ parameter.  Can we simplify AddTestCase() so it only need to pass in the name of the unit test case, and the framework can append Package  and the unit test case name?
> I see the use of the ‘Fw’ variable as a shorthand for ‘Framework’.  Can we use something other than ‘Fw’.  It may be confused with ‘Firmware’.
> UnitTestPkg/Include/UnitTestTypes.h
> I see several hard coded string lengths.  Since this runs in a host environment and strings can always be allocated, can the hard coded lengths be removed?  Update the structs to use pointers to strings instead of string arrays, and the framework can manage alloc() and free()?
> How are Fingerprints used?  The idea of using as hash as a unique identifier is a good idea.  How is the hash calculated?  What unit test code artifacts are used so developers know what parameters must be unique?  Can we just decide to use a specific hash algorithm/size and the structure can be a pointer to an allocated buffer instead of a fixed size array to make it easy to change the algorithm/size in the future?
> Update all the strings to be ASCII?  See Unicode vs ASCII above.
> UNIT_TEST_SAVE_TEST – The last field is a variable sized array, so it can be a formal field.
> UNIT_TEST_SAVE_CONTEXT - – The last field is a variable sized array, so it can be a formal field.
> UNIT_TEST_SAVE_HEADER – Can the last 3 fields be changed to pointers and be formal fields?
> Do the structures really need to be packed?  Specially with the changes suggested above?  Is the intent to potentially share data stored on disk between different host execution environments that may have different width architectures?
> UnitTestPkg – UnitTestLib.h
> Can you provide more context for the APIs SaveFrameworkState(), SaveFrameworkStateAndQuit(), SaveFrameworkStateAndReboot(), SetFrameworkBootNextDevice().  I do not see any Load() functions, so how would a set of tests be resumed?  If these do not apply to host based tests, should these be split out to a different lib class?
> UnitTestPkg/Library/UnitTestLib
> I see an implementation of MD5.  We should not do our own.  We should use an approved implementation such as OpenSSL.
> UnitTestPkg/Library/UnitTestTerminationLibTbd
> Do we really need this lib instance now?
>
> Thanks,
>
> Mike
>
> From: devel@edk2.groups.io <mailto:devel@edk2.groups.io> <devel@edk2.groups.io <mailto:devel@edk2.groups.io>> On Behalf Of Bret Barkelew via Groups.Io
> Sent: Thursday, November 21, 2019 11:39 PM
> To: devel@edk2.groups.io <mailto:devel@edk2.groups.io>
> Subject: [edk2-devel] EDK2 Host-Based Unit Test RFC (Now with docs!)
>
> Now that CI has landed in edk2/master, we'd like to get the basic framework for unittesting staged as well. Target intercept date would be immediately after the 2019/11 stabilization, so we wanted to go ahead and get comments now.
> 
> The host unittest framework consists of five primary pieces:
> - The test library (Cmocka)
> - Support libraries (Found in UnitTestPkg)
> - The test support plugins (HostUnitTestComilerPlugin, HostUnitTestDxeCompleteCheck, HostBasedUnitTestRunner)
> - The configuration in the package-based *.ci.yaml file and package-based Test.dsc
> - The tests themselves
> 
> We have a demo branch set up at:
> https://github.com/corthon/edk2-staging/tree/edk2-host-test_v2 <https://github.com/corthon/edk2-staging/tree/edk2-host-test_v2>
> We also have a demo build pipeline at:
> https://dev.azure.com/tianocore/edk2-ci-play/_build?definitionId=36&_a=summary <https://nam06.safelinks.protection.outlook.com/?url=https%3A%2F%2Fdev.azure.com%2Ftianocore%2Fedk2-ci-play%2F_build%3FdefinitionId%3D36%26_a%3Dsummary&amp;data=02%7C01%7CBret.Barkelew%40microsoft.com%7C3ce0b4eaf6d14de8822808d769f2c5fd%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C637094365265670742&amp;sdata=HFBmk%2FdB5pXI3exxB82pTS1oF877fLsrrdcirOzCCw0%3D&amp;reserved=0>
> 
> The current demo branch contains a single test in MdePkg for SafeIntLib (module file MdePkg\Test\UnitTest\Library\BaseSafeIntLib\TestBaseSafeIntLib.inf). This test is automatically detected by the HostUnitTestComilerPlugin and run by the HostBasedUnitTestRunner as part of the CI process.
> 
> A few notes about the current demo:
> 1) The demo currently only works on Windows build chains, but there's no reason to believe that it can't work equally well on Linux build chains, and are happy to work with anyone to get it there.
> 
> 2) The demo currently has four failing conditions that can be seen towards the end of MdePkg "Build and Test" log file for this build:
> https://dev.azure.com/tianocore/edk2-ci-play/_build/results?buildId=2813 <https://nam06.safelinks.protection.outlook.com/?url=https%3A%2F%2Fdev.azure.com%2Ftianocore%2Fedk2-ci-play%2F_build%2Fresults%3FbuildId%3D2590&amp;data=02%7C01%7CBret.Barkelew%40microsoft.com%7C3ce0b4eaf6d14de8822808d769f2c5fd%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C637094365265670742&amp;sdata=HgPEoYg%2Fpx0fv4J5ULO1p0kvLfqcySkJHAYxp9GB598%3D&amp;reserved=0>
> "WARNING -   Test SafeInt16ToChar8 - Status
> d:\a\1\s\MdePkg\Test\UnitTest\Library\BaseSafeIntLib\TestBaseSafeIntLib.c:302: error: Failure!
> WARNING - TestBaseSafeIntLib.exe Test Failed
> WARNING -   Test SafeInt32ToChar8 - Status
> d:\a\1\s\MdePkg\Test\UnitTest\Library\BaseSafeIntLib\TestBaseSafeIntLib.c:638: error: Failure!
> WARNING - TestBaseSafeIntLib.exe Test Failed
> WARNING -   Test SafeIntnToChar8 - Status
> d:\a\1\s\MdePkg\Test\UnitTest\Library\BaseSafeIntLib\TestBaseSafeIntLib.c:1051: error: Failure!
> WARNING - TestBaseSafeIntLib.exe Test Failed
> WARNING -   Test SafeInt64ToChar8 - Status
> d:\a\1\s\MdePkg\Test\UnitTest\Library\BaseSafeIntLib\TestBaseSafeIntLib.c:1456: error: Failure!"
> These failures seem to be legitimate and further work should be done by the community to decide whether the test case is correct or the library is correct, but one of them needs to change.
> 
> 3) Current demo pulls in test collateral from a fork of the edk2-test repo. This repo is currently *very* heavy due to the history of the UEFI SCT project and the number of binaries that it pulls down. It's possible that we (the community) need to select a better place for this code to live. Maybe in edk2 primary (though it's not explicitly firmware code, so it seems unnecessary). Maybe in a new edk2-test2 repo or something like that.
> 
> There’s an RFC doc here: https://github.com/corthon/edk2-staging/blob/edk2-host-test_v2/Readme-RFC.md <https://github.com/corthon/edk2-staging/blob/edk2-host-test_v2/Readme-RFC.md>
> And a usage guide here: https://github.com/corthon/edk2-staging/blob/edk2-host-test_v2/UnitTestPkg/ReadMe.md <https://github.com/corthon/edk2-staging/blob/edk2-host-test_v2/UnitTestPkg/ReadMe.md>
> 
> Once again, would love to get this into EDK2 master after stabilization, and most of this has previously been shopped around in other discussion threads. This is just where the rubber meets the road and is the minimal subset of code that needs to land for folks to start contributing unittests against the core libraries that can be run as part of any CI pass.
> 
> Thanks!
> - Bret
> 
> 


[-- Attachment #2: Type: text/html, Size: 33558 bytes --]

  reply	other threads:[~2019-12-04 17:50 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-11-22  7:39 EDK2 Host-Based Unit Test RFC (Now with docs!) Bret Barkelew
2019-12-02 23:12 ` Michael D Kinney
2019-12-04 17:50   ` Andrew Fish [this message]
2019-12-04 18:24     ` [EXTERNAL] Re: [edk2-devel] " Bret Barkelew
     [not found]     ` <15DD3E3A746110DF.27746@groups.io>
2019-12-06 22:21       ` Bret Barkelew
2019-12-14  0:46         ` Bret Barkelew
2019-12-14 20:07           ` Bret Barkelew
2019-12-16 22:31             ` [edk2-rfc] " Michael D Kinney
2019-12-16 23:53               ` Bret Barkelew
2019-12-17  2:19                 ` Michael D Kinney
2019-12-17  3:07                   ` Bret Barkelew
2019-12-15  0:38           ` Yao, Jiewen
2019-12-16 21:28             ` Michael D Kinney
2019-12-16 22:03               ` Yao, Jiewen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=0F460E19-2A3B-4FEE-8B6F-BE65B9C5F1D4@apple.com \
    --to=devel@edk2.groups.io \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox